Control + space key value

hi, how do I check if control key and space key are pressed ?

  • space key value is hex’20’
  • control + space, key value = hex’C2A0’

why is the value of keyboard key different ?

I would first check for space, then check if Keyboard.AsyncControlKey is True.

The AltKey (Option Key) just like Windows (Command), Shift, Lock-Shify, etc. are called Modifier Keys. You can see what they do using an OS application called ??? (on Windows) and Keyboard (on macOS). That application dislay a key board with the characters,and theyese characters changes when you press one of these keys.

Go to the documentation and read the Jeyboard page.

You do not say you look there, so I suppose that you do not read it.

Keyboard

Look at the two Properties: AltKey and AsyncAltKey.

BTW: on macOS Option-Space = Non Breaking Space (looks like a standard space but stick the tso parts of the word together - like glue-).

  • if I only hit the space key, following statement works
    If key <> space Then Return

  • if I hit space and option key, keyboard key does not contain ‘space’ as I mentioned before

  • I tried Keyboard.AsyncOptionKey and Keyboard.OptionKey, neither works with hitting space key !!!

  • I checked the documentation, but it does not help, at least I could not find some useful information.

So, explain what you want to do.

Also, where are-you testing the keyboad ?

Do your test on one platform until you reeally understand the mechanism, then check the other platforms.

Strange, because I would do the other way:

If Keyboard.AsyncControlKey Then
  // Test here the other depressed key
  If Key = Chr("é) Then
    // and so on…
  End If
End If

That is how the documentation do.

Emile, I know what I’m doing, all I want to do some processing if the user presses the option key and the space bar.
hitting only the space that works.
hitting space bar and option key does not work, because the key in event keyDown has not a space !!!

What does it have?

Good !

If you know, why are-you asking ? :wink:

As I already say above Option-Space <> Chr(32) / 0x20.
You have to check against 0xC2A0.

C2A0 is the value you said you get.

  • control + space, key value = hex’C2A0’
    Where is the problem ?
Unicode code-point     UTF-8     Character description
U+00A0                 c2 a0     NO-BREAK SPACE

Try this:


Function KeyDown(key As String) Handles KeyDown as Boolean
  If Keyboard.AltKey Then
    If Key = Chr(32) Then
      MessageBox "You Hit Alt-Space."
      
    Else
      MessageBox "The pressed character is " + Asc(Key).ToString
    End If
  End If
End Function

TextArea, macOS Sequoi, Xojo 2024r4.2 tested right now (and following Docmentation yips).

Result:

The change Chr(32) to Chr(160) is done at the OS level. You cannot change that (or test the KeyCode depressed. Explanation: link already provided).

And 160 is A0 in hex - the unicode code-point for a no-break space.

And 160 is & n b s p ; in html. (without the spaces…)

&nbsp;

In what this help the OP ?

The operating system intercepts some keystrokes that never get sent to your window.

For example Cmd + Space is intercepted to open Spotlight on MacOS. Xojo does not capture this in the KeyDown event.

For me, Option + Space is another of these, as I have that keystroke mapped to my window management software.

If you want to capture these keys, you need to do it in a Timer using AsyncKeyDown.

Sub Action() Handles Action
  
  If Keyboard.AsyncKeyDown(&H31) Then //space
    If Keyboard.AsyncControlKey Then
      MessageBox "Control + Space"
      Return 
    End If
    If Keyboard.AsyncOptionKey Then
      MessageBox "Option + Space"
      Return 
    End If
    If Keyboard.AsyncAltKey Then
      MessageBox "Alt + Space"
      Return 
    End If
    If Keyboard.AsyncCommandKey Then
      MessageBox "Cmd + Space"
      Return
    End If
    MessageBox "Just Space"
    Return 
  End If
  
End Sub

Likewise, the OS can change the ASCII code sent to KeyDown based on different key combinations. This can vary based on OS version and based on software the user has installed (for example AutoHotKey on Windows), so for obscure key combinations it’s not always trustworthy to use KeyDown and you should use a Timer and directly poll the key via the Keyboard.Async* methods instead.

Here’s on macOS what you get when you press the Option key, from the Keyboard:


As you can see, there is a special key with an orange square: tyoing Opt-~-n gaves ñ. Another special case. (same peratin p-for ¨¨’`)…

Your description is about macOS using two depressed keys to perform an action.

Will this help OP ?
I think (but I may be wrong since OP do not say what he want to do) we are far away from the question.

thanks Christian, that works for me !

it is still very strange, that the value of key changes if control key is pressed !!!
that does not make any sense !!!

Not that strange actually as it has always worked this way … In the good old days of 7-bit ASCII this was really simple: If you pressed the shift key, 32 would be subtracted from the code for another key pressed at the same time. For example, pressing ‘A’ on its own resulted in an ASCII code of 97 (‘a’) while together with shift you got 97–32 = 65 (i.e. ‘A’). Ctrl worked similarly but would subtract 96 so could access control codes (0 to 31) by typing character keys, like Ctrl-C for interrupting the execution of a program (ASCII code 3 aka ETX).

These days we are dealing with a lot more characters than ASCII could provide and the way key presses map to code points is more complex but one thing hasn’t changed: holding down the shift, ctrl, or alt key will modify the code sent by pressing a second key.

On Windows:
Alt+164 for ñ and Alt+165 for Ñ

On macOS Option-~n and Option-~N.

And this is from 1984, the original Macintosh.

That‘s called a ‘dead key’. While modifier keys such as shift, ctrl, and alt/option modify the effects of other keys pressed at the same time, dead keys modify the effect of the next key pressed. Like in the case you’ve mentioned: Typing ‘~’ (accessed by typing option-N if ‘~’ isn’t on your keyboard) doesn’t produce a character on its own, but if you then type ‘N’ you get ‘ñ’. To get ‘~’ alone you first type option-N and then space.