Keyboard, events, AsyncKeyDowns, modifier keys (question)

I’m struggling to understand keyboard events, AsyncKeyDowns and modifier keys. Working on a Mac, I have a large window, call it winBig, with lots of DesktopTextFields, and a few labels, checkboxes and buttons. At any one time, one of the DesktopTextFields is pretty well certain to have the focus, because I don’t think any of the other controls gets the focus, does it?

When I hit a character key a-z or shifted A-Z, but nothing else, including numbers and other symbols, except for possibly the apostrophe, I want the text field with the focus to add it to its text, but to ignore almost every other keystroke.

I want the tab key to move the focus on to the next text field. shift-tab to move it back and delete to delete characters one at a time, backwards, in the focus field, as you would expect.

I would like the Return key to simulate a click on the default button - there will only ever be one button with default on screen.

Everything else, unused punctuation, numbers, arrow keys, function keys etc along with Cmd-character, Alt-character, Ctrl-character, Ctrl-function, Cmd-a, Ctrl -z, Cmd+Alt+key, Cmd+Alt+Ctrl+key etc should be interpreted (by winBig keydown event?) as an instruction to do something coded in a winBig Method if there is a response to the key combination.

How do I achieve what I am trying to do? If you start talking about keydown events could you please be clear whether you mean my text field keydown events or winBig keydown events. If you have the time, could you please give me an example of what I need to write for Cmd+Alt+Ctrl-a and Cmd+Alt +Ctrl+F1. I don’t quite understand why I may need to use a Timer for these. No lengthy code execution is going to be going on during this operation

Thanks if you can give me the key to all this. Steve

If the user chooses so, the focus can be on any control (checkboxes, push buttons, etc.). Look at System Preferences→Keyboard→Shortcuts to find this setting.

Since you’ll have the same code for multiple text fields, the first thing is to make a subclass.
In the “Insert” menu, choose “Class”. Name it like “CFilteredTextField" and set the super to “DesktopTextField".
In your window, select each text field and change its super to “CFilteredTextField”. At this point, all you do in “CFilteredTextField” will apply to each of your existing DesktopTextFields.

Now, for the filtering, do this:
In CFilteredTextField, add the KeyDown event handler. Put something like this:

Var a as Integer=Key.Asc 'Grab the ascii key code

if a<32 then return false 'Includes Tab, arrows, return, enter, esc, page up, page down, backspace. Let the framework handle them as usual (=the “return false” statement).

if a=127 then return false 'The Delete key. Also let the framework handle it.

if Key="'" or (a>64 and a<91) or (a>96 and a<123) then return false 'Also allow ', A-Z and a-z.

Return True 'Deny anything else.

If you’ve already done things in your project, these steps may be more complex. Just ask further if you get something not working.

OK, thanks for that, I hadn’t thought of creating a sub-class so that’s a good start. It’s a part of a bigger project designed to do a lot of things with words and phrases, so nothing in this section to undo or work around as yet! Thanks again

If I have a normal lowercase character with a modifier key as well such as Cmd-a, will that be ignored by the focus text field, allowing me to use that as an instruction?

It depends. With Option, shift and Control keys, the character is usually modified; you get the typed (modified) character in the KeyDown event.
With the command key, the character isn’t modified. But, if a menu item exists for a combination (e.g. Command-V), the field won’t receive the event at all (since it’s handled by the menu item). You may also have combinations already reserved (e.g. you won’t receive an event for Command-Tab).

Assuming a given combination isn’t reserved as a shortcut so you get a KeyDown event, you can then use these statements in the KeyDown event:
if Keyboard.CommandKey then MessageBox “Command key was pressed.”
if Keyboard.ControlKey then MessageBox “Control key was pressed.”
if Keyboard.OptionKey then MessageBox “Option key was pressed.”
if Keyboard.ShiftKey then MessageBox “Shift key was pressed.”
if Keyboard.CommandKey then MessageBox “Command key was pressed.”
if Keyboard.CommandKey then MessageBox “Command key was pressed.”

OK, got that, thanks

In Xojo, Windows CANT receive keydown events while other control has the focus. You have to do it all in text field keydown. You can simulate this to some degree having keydown events on all of your controls as Arnaud told you, BUT, to have real HotKey like you are asking for, you are going to need declares, or a plugin.

OK, if there is a keydown that Arnaud’s “CFilteredTextField” doesn’t like, such as any of the keydowns his code rejects, could the textfield transfer the focus to some object, possibly outside the window, whose sole job it is to decipher the keydown captured in variable-a along with any modifier keys that were down at the same time and take the appropriate action before transferring the focus back to the text field again or elsewhere if that is what is required?

It seems to me that would get the job done but would also keep the action code separate from the textfield, which really doesn’t care what all these these alien keydowns are about?

The textfields aren’t normally going to transfer the focus to anything other than another textfield, that will only happen if the user clicks on a checkbox or a button, and it should be easy enough for the action of these objects to conclude with passing the focus on to a text field again.


When the control receives the keydown, all the OS messages had being already translated an processed.

You may also have combinations already reserved (e.g. you won’t receive an event for Command-Tab).
These are registered for the OS.

And, a Cmd- can be free today and… at a future OS release, be taken by the OS use (reserved).

Also, wikipedia can be at help for general computer information like:

do not forget to look until the end of the documents, because you may miss:

Also a valuable help:
Human Interface Guidelines
Similar document exist for the Linux and Windows OS (search them by yourself).