0

I'd like to detect Shift+3 in the Form's KeyDown event. KeyChar contains '3' when just the 3 key is pressed, but is modified to the Shift character when Shift is down (on my British keyboard it's the '£' symbol but for US keyboards it will be '#' and OSX could be '"' and so on). How can I detect this for any keyboard for Windows or Mac? The Key input variable is empty unfortunately. I looked for a function to convert a character to a virtual key code (such as Windows VkScanEx) but didn't find anything for both Windows and OSX. I somehow need to convert the Shift modified character back into a '3'.

Here's a relevant quote from the documentation. "If a pressed key combination can be treated as a printable character or digit, then Key = 0 and KeyChar contains a pressed symbol" http://docwiki.embarcadero.com/Libraries/Rio/en/FMX.Controls.TControl.KeyDown

This is the cause of my issue. I need to interpret the key press as a shortcut rather than a printable character. So I somehow need to get the virtual key value. This would not be a problem for Ctrl+3 because it would be interpreted as a shortcut and Key would have the correct non zero value. Unfortunately I need to detect Shift+3.

Note that on some keyboards (e.g. French) I would have the same problem without the Shift key being pressed since the numbers and symbols are reversed with the Shift key. I'd like to detect the 3 key being pressed whether or not Shift is pressed and on any keyboard.

XylemFlow
  • 963
  • 5
  • 12
  • Not sure I understand what you say... Some keyboards have explicit hardware/firmware preventing simultaneous key presses for keys you normally don't want to use simultaneously. Usually "gamer" keyboard permit pressing several keys. If that is not what you are asking, maybe you are asking about keyboard layout. I'm using an AZERTY-BE keyboard layout. The key having 3 on it produce double quote if used alone, number 3 if used with shift and hash (#) if used with ALT-GR key. You can query the OS to know which keyboard layout is configured. – fpiette Dec 08 '20 at 14:52
  • 1
    @fpiette: In a VCL application, it is trivial to use `TForm.OnKeyDown` to handle Shift+3. I just tried to do the same thing in a FMX application, and wasn't able to make it work. I assume this is what the OP is asking about. – Andreas Rejbrand Dec 08 '20 at 15:05
  • Yes Andreas that's what I'm trying to do. fpiette, I forgot that French keyboards return the symbol without the Shift key pressed, in which case I'd have the same problem without the Shift key being pressed (which I also want to detect). Basically I'd like to know if the 3 key is pressed whether or not the Shift key is pressed and on any keyboard. – XylemFlow Dec 08 '20 at 15:36
  • Why bother with shift? The user know how to produce a "#", a "3" or a double quote on his keyboard. On my Android phone the shift is a kind of shift lock and there are "pages" where I can select specific characters. So your application has just to handle OnKeyPress (not OnKeyDown) and see which character code is passed: a "#", a "3" or a double quote. Do I still not understand the issue? – fpiette Dec 08 '20 at 15:49
  • I don't see an OnKeyPress event in FMX. In my application the 3 key is being used as a shorcut to zoom into a particular object on a canvas (similar to Inkscape). I want the 3+Shift combo to cause a slightly different behaviour. So I need to know if the 3 key is pressed and also if Shift is pressed. I can't check all possible characters that could be on the 3 key because it could cause false detection. – XylemFlow Dec 08 '20 at 16:08
  • Tom, I mean on Windows. The documentation states that either the KeyChar is populated with a non zero value or the Key is a non zero value, not both. KeyChar is populated in this case because it's interpreted as a character input rather than a shortcut input. My problem is that I'd like to interpret it as a shortcut input. Are you saying that the Key input is non zero for you when pressing 3 or Shift+3? "If a pressed key combination can be treated as a printable character or digit, then Key = 0 and KeyChar contains a pressed symbol". – XylemFlow Dec 09 '20 at 15:43
  • 1
    Ouch! I'm sorry, @XylemFlow. I made a test case mistake and I'm wrong. I will delete my previous comment soon. Once again, sorry. – Tom Brunberg Dec 09 '20 at 20:59
  • No problem. You're right that I need to use virtual key codes though. I've discovered that the problem is even worse when using French keyboard settings as the number keys can't be used as shortcuts at all. I don't understand why the KeyDown event can't provide both the character and the key code. I'm trying to replicate the zoom shortcuts in Inkscape (1 sets zoom to 100%, etc). In Inkscape it works with any keyboard settings. This seems like a very simple problem. If I can't find a solution then I may be forced to use the function keys instead but that's not ideal. – XylemFlow Dec 10 '20 at 11:58
  • Should'nt you be using KeyUp Event instead of KeyDown event? I get [shift] and '#' for KeyChar. Then you can test for other languages and OS's. – Doug Rudd Dec 13 '20 at 01:55

0 Answers0