Windows 8.1 Handling Physical Keyboard Input - windows

I'm developing a Universal App in c# for Windows 8.1
I'm trying to catch keyboard input and from the on-line literature have been pointed to KeyDown event and KeyRoutedEventArgs and the Key property. This works fine for most key characters and returns VirtualKey enumerations such as VirtualKey.A. However, when characters such as ,.;:> are pressed the Key property is set to a number and this number is the same value if the key is pressed in combination with VirtualKey.Shift which seems to indicate it is not representative of the characters on the key, but only represents the physical key.
As it happens, I'm using an Italian laptop keyboard which of course is laid out differently to a UK Extended keyboard so the Key value for a semicolon (;) is 186 on a UK keyboard (as an un-Shifted or bottom character) and is 188 on an Italian keyboard (as a Shift or top character). I'm therefore stumped as far a capturing these punctuation characters.
I have developed WPF, Silverlight and .Net app in the past and don't remember it being as difficult to handle physical keyboard input even in combination a game loop, as I just used the Key or Keys enumeration and an OEM value.
How can I capture these punctuation characters when their respective key is pressed, irrespective of the keyboard layout?

Answer provided by Hans Passant (https://stackoverflow.com/users/17034/hans-passant):
Characters typed on a physical keyboard are exposed by this event:
http://msdn.microsoft.com/en-us/library/windows.ui.core.corewindow.characterreceived.aspx

Related

Creating Barcode to "Alt+Tab" and switching windows application, and Enter Key

I would like to ask for some help with firstly how to define the windows keys "Alt+Tab" and "Enter" key when using an online barcode generator like https://barcode.tec-it.com/en. Alternatively, if there isn't a way to define the keyboard commands, is there a place that I can find these commands in the form of Barcodes? Code-128 is preferred.
Thank you
Barcodes simply contain ASCII (or with more modern 2d symbols) UTF-8 character codes. So standard keyboard values such as Enter and Tab that correspond to ASCII values can be encoded in a barcode.
On the other hand, dedicated barcode scanners that attach via USB are essentially keyboard emulators. And those can be programmed to create Alt and Ctrl key sequences.
Unfortunately, I do not know of any scanner makers that support Alt-Tab. Most programmable scanners can create Alt-A ... Alt-Z but not Alt-Tab.
Additionally, most barcode scanners allow you to define prefix and suffix key codes to send when a barcode is scanned. The most common configuration is to send Enter or Tab after each scanned barcode. This is done by programming the scanner, not in the barcode.

How does one keyboard press lead to different characters, depending on layout

Thank you for taking the time to look at this question.
If you press the ‘y’ key on a keyboard, it is my understanding, that a UTF-8 keycode is sent over USB corresponding to the ‘y’ key. i.e. 0x79 in hex.
This is then interpreted by the computer as the letter ‘y’ and sent to the active window as a keystroke.
My question is, how come when you plug a US keyboard into a German computer, pressing the ‘y’ key results in the letter ‘z’ being sent to the active window.
Now on German keyboards, the letter ‘z’ is in the position which the ‘y’ key is in on a US keyboard. However, if each character has a unique UTF-8 code associated with it, surely pressing the ‘y’ key will generate the ‘y’ keycode (0x79) and this will be interpreted as a ‘y’ by the computer. Surely the location of that key on the keyboard shouldn't matter?
I assume there must be interconversion at some point to account for different layouts. How and at what point does this interconversion happen, is it done by the microcontroller in the keyboard or by the computer when it receives the keycode?
Please feel free to point out any wider misunderstandings in the above questions if they are present.
Many thanks for your time.
There are a couple of things your computer is aware of, first is your keyboard layout, which is usually asked about (in Ubuntu during installation for example) or detected automatically (like in Windows for example) by the default keyboard driver. The second is the system language, or preferred language, or keyboard language depending what its called in your Operating System.
When you plug in your English keyboard, the system recognizes it as an English keyboard and will look for your selected language and map the keys accordingly. I type in both English and Arabic, the keyboard layouts are totally different, and all I do is choose from the my keyboard icon in the taskbar that i want to type in Arabic now, the system still uses my English keyboard but with an Arabic keymap.
Hope this explains it!

Hotkey field in XUL for Firefox

I have a Firefox extension written in XUL. It takes a hotkey. I want to have an input field where the user can press any key and I can programmatically determine what key was pressed.
I don't care about key combos which have meta meanings inside the OS (I don't need to capture Ctrl + Alt + Del).
If the key is already defined by Firefox, I'm OK with not capturing it (for instance, if the user presses F1 and help pops up instead of the key being captured, I'm fine).
I would also love to have some way to determine programmatically whether the key pressed already has some other meaning inside of Firefox, but that is likely out of scope for this request. I just mention it in case it's easy.
My current solution, which seems to have been adopted by a number of add-ons, is to provide a text box where the user can type a printable character and then a series of check boxes for modifier keys. This solution is barely workable but terrible for a number of reasons (it doesn't allow users to use non-printing keys such as function keys in their combos, it allows the user to input invalid characters (such as Unicode characters with no physical key on the keyboard), and it's just awkward to use).
Thanks!
You can use Inline Options
https://developer.mozilla.org/en-US/Add-ons/Inline_Options
You can detect the key pressed in the text input and write your modifiers like accel or alt.
Source example:
http://git.io/vez1o

How to determine if a certain key is pressed, knowing only its position on U.S. keyboards?

Consider that, for a Windows video game, I need to determine if the key which generates the ` and ~ characters on the U.S. English keyboard layout (which is usually below the Escape key and left to 1) has been pressed. This may sound like a trivial question, but it doesn't seem like one to me.
When Windows sends keyboard messages, it specifies the virtual key code and the OEM scan code. We can't rely on the OEM scan code, because "the value depends on the OEM" - and nor can we depend on the virtual key code, because it depends on the currently active keyboard layout.
Our current "solution" is to use LoadKeyboardLayout and MapVirtualKeyEx to find the OEM scan code of the key that generates the ` character on the U.S. English keyboard layout, then just listen for that OEM scan code. The problem is that this doesn't work if the user doesn't have the U.S. English layout installed.
Is there a real way to do this on Windows?
The OEM scancode does not change from keyboard to keyboard. No reason not to use it.
Back in the DOS-days the same scancodes have been used for games because it has been the only way to detect key-up and key-down events. Noone had problems with it and I doubt it will change in the future.
If you want another option you may want to give DirectInput-API a try. It gives you the raw scancodes as well and if I'm not mistaken you can also query the physical position, dimension and whatnot of each key.
Tie the game action to the character, not the position of the key. Otherwise, how do you tell the user which key to press? "under the escape key"? They may not have anything there, but if you tell them "the ^ key", they can look for it.
You'll probably also want to make it configurable to accomodate exotic keyboard layouts and user preferences.

Using Numpad with Modifier Keys exhibits curious behavior

I have a keyboard event listener, and I am listening for the number pad key codes (1 through 9) for when number lock is activated; this works fine. However, in my app I also want to allow usage of a modifier key (CTRL) along with the number pad keys. The strange thing is that when holding CTRL, pressing 1 or 3 does not generate any keyboard event whatsoever, whereas 2 and 4 - 9 do generate the expected events. I have seen other references to this issue after some Googling, so I do not believe this is necessarily Flash-specific, but I have yet to find any answers.
I tried using SHIFT as a modifier, but that just results in generating the key codes from the number pad as if number lock was off (eg, SHIFT+Numpad1 returns the End keycode, regardless of number lock state) - apparently this is intentional Windows behavior. ALT is not an option with the numpad due to altcodes.
Any ideas on how to get CTRL+Numpad1 and CTRL+Numpad3 to generate the keyboard events? Or any explanation as to why they don't?
Edit: I tried out using these key combinations in Firefox as enriquein suggests below, and all the key combinations work A-OK, leading me to believe that this is likely a Flash-specific issue, or at least not a hardware issue.
I have run into issues in the past with numpad keys on specific keyboards. Various key combinations fail to register on specific keyboards, and it is possible that the keyboard you are using may just not be generating any event in that case. It's not guaranteed to work, but I would recommend trying out a different keyboard (different vendor, etc) and see if that works. It also might be an issue with localized keyboards, if you are using a non-english keyboard.
I'm almost certain it's not Flash related because I had similar problems with a localized keyboard and could not get it to generate events at all for certain keys or key combinations.
Try enumerating all key events and searching trough what they're mapped to, or google evtest.c, compile it and run it and see what it has to say.
Indeed it looks like its keyboard specific. I tried the key combinations as I read this question in Firefox and they triggered the same event as pressing Ctrl+Number (which is switch to tab #Number).
This was using a standard US/101-key english keyboard (no extra media buttons or anything).
Some keyboards don't register certain key combinations. I think that this is down to how they are put together. For example some may register left hand Ctrl + key combination but not right Ctrl + same key!
A work around would be to detect the key down and key up events for the Ctrl key.
You could then raise a custom event for Ctrl + numpad key or work with a volatile flag to show the Ctrl key state.

Resources