Listbox keyboard shortcuts

Is there a way to handle keyboard shortcuts in a listbox?

I need some way to handle something when a user uses CTRL+E
You can of course use the Keydown event but this does not accept CTRL, ALT, … keys. So no way to know if CTRL+E has been hit.

[quote=207715:@Christoph De Vocht]Is there a way to handle keyboard shortcuts in a listbox?

I need some way to handle something when a user uses CTRL+E
You can of course use the Keydown event but this does not accept CTRL, ALT, … keys. So no way to know if CTRL+E has been hit.[/quote]

What about simply adding a specific menu when the Listbox has focus ? Then you can have shortcuts without extra coding.

Otherwise use keydown, but instead of looking at key, use Keyboard.AsyncKeyDown.

[quote=207717:@Michel Bujardet]What about simply adding a specific menu when the Listbox has focus ? Then you can have shortcuts without extra coding.

Otherwise use keydown, but instead of looking at key, use Keyboard.AsyncKeyDown.[/quote]

Thanks for the tip but forgotten to add: I don’t want any menubar appearing. :slight_smile:

Putting the below code in the Keydown event does seems to work. Not sure if this the correct way to do this.

Seelct Case ASCB(key)
case 5
msgbox “CTRL-E pressed”
end select

return true

My solution is only working for Windows.
With OSX I need the CMD+C and it seems the CMD is not catched. Probably because it is already in use by the OS itself (for copy/paste).

You should listen to Michel and read up on AsyncKeyDown.

What is the point of giving you advice if you just ignore it? What is the point of asking for advice in the first place?

First note, that listbox copies automatically the selected cells when you do Ctrl-C / Cmd-C.

But if you want to change the behavior, just implement the menu handler (I think it is called EditCopy) either in the listbox subclass or in the window.

If you do it in the window you need to separate listbox copy from other controls by doing:

Function EditCopy // the menu handler in the window If Focus = Listbox1 Then // do your special copying here Return True // prevents standard copy Else Return False // let Xojo handle all other controls End End

[quote=207735:@Christoph De Vocht]My solution is only working for Windows.
With OSX I need the CMD+C and it seems the CMD is not catched. Probably because it is already in use by the OS itself (for copy/paste).[/quote]

This works for me in OSX

if Keyboard.CommandKey then if key = "e" then msgbox "CMD-E pressed" Return true end if end if

[quote=207736:@Markus Winter]You should listen to Michel and read up on AsyncKeyDown.

What is the point of giving you advice if you just ignore it? What is the point of asking for advice in the first place?[/quote]

I did. :slight_smile:
But thank you for pointing that out in a very friendly way. :slight_smile:

[quote=207744:@Axel Schneider]if Keyboard.CommandKey then
if key = “e” then
msgbox “CMD-E pressed”
Return true
end if
end if[/quote]

That work fine for OSX. Thanks.

BTW the Keyboard.AsyncKeyDown does not seems to work reliable for OSX (as far as I tried).

[quote=207746:@Christoph De Vocht]That work fine for OSX. Thanks.

BTW the Keyboard.AsyncKeyDown does not seems to work reliable for OSX (as far as I tried).[/quote]

Christoph, you should REALLY read http://documentation.xojo.com/index.php/keyboard

Keyboard.CommandKey for instance tells you Command has been pressed. Then all you need to do is something like this in KeyDown, as Axels told you too :

If Key = "e" and Keyboard.CommandKey then msgbox "Command key has been pressed" end if

It works fine for Alt and Option as well.

Ctrl-key as you discovered simply shifts a bit, so you can go on with what you did.

To be exact, Control characters are obtained by subtracting 64 to the ASCII value of the uppercase character, not by shifting bits. Sorry.