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

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!

Related

Supporting keyboard shortcuts while typing into NSTextView?

This is more a best practices/usability question around the behavior of MacOS apps.
I have an app where a user is constantly typing into a NSTextView (think text editor).
I want to support a few keyboard shortcuts for common tasks within the app so that the user does not need to reach for the mouse to click a button.
But because the user is also typing into a text view, this is a bit tricky figuring out what combinations are available.
All single character keys are off limits (since the user will actually want to type that character).
Most cmd+key combinations are also taken (cmd+c, cmd+left, cmd+right, etc).
What's the expected behavior/best practice here?

Replace any keyboard character(s) with keyboard shortcut or different keystroke

For a project using a barcode scanner I need to know if it is possible to replace a special character like
!
"
§
$
%
=
with different keyboard strokes like
arrow down or arrow up or even shortcuts like
ctrl+a or ctrl+v?
Would be also possible if a specific series of characters resulted in a keystroke/keyboardshortcut,
for instance this text InsertArrowLeftHere would result in this keypress arrow left
Is there any way to make something like this work?
A bar-code scanner (unless is used with special hardware in-between) just reads the data, and sends keystrokes to the computer as keyboard interrupts. How the bar-code "string" is interpreted depends on software that, at that moment, has the focus. If you open a Notepad and read something with the barcode scanner, the number will be printed in notepad. In many cases no software comes with the scanner because there is no need for that.
But your software (the program that receives the data from the scanner) can catch anything typed in the textbox (or other control that has the focus, for example the whole form can catch the keystrokes). Maybe you can also identify where the keystrokes come from, (means: from which keyboard-input device: keyboard1, keyboard2, barcodescanner etc.) and act accordingly (if from keyboard1 or keyboard2 do nothing, if from barcodescanner then do this).

Windows 8.1 Handling Physical Keyboard Input

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

Is ctrl-s save universal for save or are there different combinations for different languages

I imagine that there are different combination, just looking for an affirmation. Or, are we developers really that english-centric?
If I were in Japan, using a word processor on some computer, would Ctrl + S save?
Are there keyboards where Ctrl + S doesn't even exist (non-english keyboards)? Most of what I've seen usually has latin + [insert language here] characters, usually overridden on the normal qwerty keyboard.
It depends.
If the application's UI is in English, most people will expect CTRL + S to save.
If you're writing an application that is going to be translatable, you probably want also the shortcuts to be translatable. Shortcut standard varies between languages and countries. Leave this to the translator.
Yes, Ctrl- and Alt- are usually passed on verbatim and not subject to IME processing—assuming one is using an IME; I don't know about IME-less JP input methods. But in the basic case where an IME is used, Ctrl-S remains Ctrl-S, it does not become Ctrl-す or something.
Furthermore, menu entries often have a Latin accelerator defined - cf. http://www.marsei.net/tec/tecladoEspWindows.jpeg :
addmenuentry(m, "ファイル(&F)");
Just tried on my Mac by setting Spanish as my default language. None of the programs I opened used different shortcuts in Spanish than English, although the menu text was all translated. TextEdit was the main program I used to test.
I also tried setting my keyboard to the Dvorak layout (very different layout, still meant for English), Spanish layout (still QWERTY), and French (a & q, z & w, m & , & ; swapped). Even though the French keyboard had the q in a different place, the shortcut was still CMD-Q.
So, it seems the convention on Mac OS X is to have the shortcuts always use the same letters.
However, according to this link, in Spain CTRL-G(uardar) is normally the shortcut for saving: http://www.ixda.org/node/18527.
Here's an argument for basing the shortcuts on the position of the key on the keyboard, but I'd be surprised if anyone actually does that. http://blog.i18n.ro/are-we-supposed-to-localize-keyboard-shortcuts/
In Visual Studio, CTRL + S saves the document. In MS Office it's the same if it's in English, otherwise if it's in Italian, CTRL + S underlines the text, while SHIFT + something saves the document. I have no idea why they did this, but it might also apply to other applications and languages.
It depends on what you want to write. if you want to write a program and want to publish it everywhere, you should decide if you want it to write in only one language or in lots of languages. If you only want to write it in English, every one that wants to use it should change his/her system language to English or at least should support English. In that way Ctrl + S will do what you want.

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.

Resources