How to use "If key = Keyboard.keyname"

I’m not sure of the exact formulation of this, or even if it’s possible.
I’m trying to use the info in “Keyboard.keyname” to make an if statement to replace the statement in KeyDown event

If Key = ChrByte(28) Then//Left arrow Key

It says [quote]This returns a value that indicates the character the key produces. In most cases, special keys will be named as follows: Esc, Tab, CapsLock, Shift, Control, Option, Alt, Command, Win, Return, Enter, Space, Fn, Delete, Backspace, Help, Insert, Home, PageUp, Del, End, PageDown, F1 through F15, PrintScreen, ScrollLock, Pause/Break, Clear, NumLock, KP=, KP/, KP*, KP-, KP0 through KP9, Up, Left, Down, and Right.[/quote]

And it follows up with this piece of code (which implies something is possible).

MessageBox(Keyboard.keyname(&h7D))

And MessageBox is supposed to print “Down”

So. I wonder if I can create something like

If left = key Then

to replace

If Key = ChrByte(28) Then

So why am I asking instead of experimenting? Because alot of times I learn different ways of doing something.

ChrByte does not exists @ docs web site.

ChrB gives a link to String.ChrByte…

Thanks Emile. I saw String. somewhere and wondered how I got that ChrByte from autocomplete.

[quote=497651:@Arthur Gabhart]
So why am I asking instead of experimenting? Because alot of times I learn different ways of doing something.[/quote]
Create a module, name it Keys (since you cannot use an existing module name)
In there put a bunch of computed properties
Left, Right
You’ll probably need to make them all public which mean when you want to use one you have to use the full name - Keys.Left or Keys.Right etc

In each computed property have that return Chrb( xxx ) for whatever key its supposed to be

Write this once and use that all over

Then you can do something like

If Keys.left = key Then

Just fyi, keycodes are different than chrb(xxx). Keycodes represent physical key positions on the keyboard, chrb() values represent ascii byte values.

Thanks Tim. I was just curious if this was an undocumented feature. It isn’t.
If you press a down arrow key, and you have this code in keydown

MessageBox(Keyboard.keyname(&h7D))

you do get “Down”

If you have this code

If Keyboard.keyname = "Down" Then MessageBox(Keyboard.keyname(&h7D))

you only get an analyzer error. absolutely expected unfortunately.

If Keyboard.AsyncControlKey and Keyboard.AsyncKeyDown(07) then
msgbox “Control-X”
elseif Keyboard.AsyncControlKey and Keyboard.AsyncKeyDown(08) then
msgbox “Control-C”
Dim C as New Clipboard
C.Text = Me.text
msgbox C.Text
elseif Keyboard.AsyncControlKey and Keyboard.AsyncKeyDown(09) then
msgbox “Control-V”
elseIf Keyboard.AsyncKeyDown(&h7B) Then
// do something with the left arrow key
msgbox “Left arrow Key”
elseIf Keyboard.AsyncKeyDown(&h7C) Then
// do something with the right arrow key…
msgbox “Right arrow Key”
elseIf Keyboard.AsyncKeyDown(&h7D) Then
// do something with the down arrow key…
msgbox “Down arrow Key”
elseIf Keyboard.AsyncKeyDown(&h7E) Then
// do something with the Up arrow key…
msgbox “Up arrow Key”
End If
Return True

//https://documentation.xojo.com/api/hardware/keyboard.html#keyboard-asynckeydown

Lennox

Just don’t use AsyncKeyDown in a KeyDown event. Use the Key value you are passed.

Hi Tim,

Could you explain why and how should I modify this

elseIf Keyboard.AsyncKeyDown(&h7D) Then // do something with the down arrow key... msgbox "Down arrow Key"

Thanks.
Lennox

Amidst all this, it would be nice if there was a way in Xojo to get the KeyCode of the pressed key without having to query the OS or try them all hoping to get lucky. I want to be able to do something in the KeyDown event handler like

Var kc as Integer kc = KeyBoard.KeyCode MessageBox(Keyboard.keyname(kc))

[quote=497778:@Dale Arends]Amidst all this, it would be nice if there was a way in Xojo to get the KeyCode of the pressed key without having to query the OS or try them all hoping to get lucky. I want to be able to do something in the KeyDown event handler like

Var kc as Integer kc = KeyBoard.KeyCode MessageBox(Keyboard.keyname(kc)) [/quote]
Seems like a decent feature request since keycodes are specific to a given keyboard layout

[quote=497734:@Lennox Jacob]Hi Tim,

Could you explain why and how should I modify this

elseIf Keyboard.AsyncKeyDown(&h7D) Then // do something with the down arrow key... msgbox "Down arrow Key"

Thanks.
Lennox[/quote]
Why: KeyDown events are queued by the system and the then-current keystroke is recorded and passed into the event in the Key parameter. The status of the shift, alt and command keys are available in the non-Async versions of the Keyboard object. The Async versions of the Keyboard object read the keyboard in real time. It is possible for the Async values to get out of sync with the KeyDown events given a fast typist and slow system.

How:

elseif Key = ChrB(31) Then
  // do something with the down arrow key
  msgbox "Down arrow Key"
end

I filed a feedback suggestion about this a while back but it never got past “Reviewed”.
<https://xojo.com/issue/33276>

This would have to return an array. And you’re basically asking Xojo to try them all and report back to you. The concept of “the key that was pressed” makes the most sense in a KeyDown event. Otherwise, there could be multiple keys depressed simultaneously and you’d have to sort them out.

Thanks Tim.

Lennox

Something like an NSEvent which would definitely be possible to abstract x-platform

True. I hadn’t really thought about situations where multiple keys are pressed.