

So hotkeys that rely on it work well even in case of a language switch.ĭo we want to handle layout-dependant keys? Then event.key is the way to go. On the other hand, de has the benefit of staying always the same, bound to the physical key location. To reliably track layout-dependent characters, event.key may be a better way. You can find the list in the specification. keyA, keyQ, keyZ (as we’ve seen), and doesn’t happen with special keys such as Shift. Luckily, that happens only with several codes, e.g. Same letters in different layouts may map to different physical keys, leading to different codes. So, de may match a wrong character for unexpected layout.

The specification explicitly mentions such behavior. If we check de = 'KeyZ' in our code, then for people with German layout such test will pass when they press Y. Literally, de will equal KeyZ for people with German layout when they press Y. So it makes sense to check de, it’s always the same.įor the same key, US layout has “Z”, while German layout has “Y” (letters are swapped). If the visitor has several languages in OS and switches between them, the same key gives different characters. On one hand, the value of event.key is a character, it changes depending on the language. There’s a dilemma here: in such a listener, should we check the value of event.key or de? We can set a listener on keydown and check which key is pressed. Most text editors hook the “Undo” action on it. Let’s say, we want to handle a hotkey: Ctrl +Z (or Cmd +Z for Mac). The de tells us exactly which one was pressed, and event.key is responsible for the “meaning” of the key: what it is (a “Shift”). For instance, most keyboards have two Shift keys: on the left and on the right side. Please note that de specifies exactly which key is pressed. For those keys, event.key is approximately the same as de: Key What if a key does not give any character? For instance, Shift or F1 or others. The check like de="keyZ" won’t work: the first letter of "Key" must be uppercase. Please evade mistypes: it’s KeyZ, not keyZ. Seems obvious, but people still make mistakes. That will become the value of event.key, while de is always the same: "KeyZ". If a user works with different languages, then switching to another language would make a totally different character instead of "Z". The event.key is exactly the character, and it will be different.


That gives us two different characters: lowercase z and uppercase Z.
Keyup enter code#
The key property of the event object allows to get the character, while the code property of the event object allows to get the “physical key code”.įor instance, the same key Z can be pressed with or without Shift. The keydown events happens when a key is pressed down, and then keyup – when it’s released. The question is not clear enough you would need to start with explaining the purpose of what you are doing so, you can get more help if you further clarify the question.Focus on the input field and press a key. I doubt that handling KeyUp and checking up Enter makes any practical sense at all. If you need assign true to the property Form.KeyPreview and handle the event PreviewKeyDown or override the method OnPreviewKeyDown: There is another problem of handling keyboard events on the forms: you probably have some controls which grab keyboard focus, so the KeyDown event is not dispatched to the form itself. And using auto-generated names of methods permanently is against good programming style, as such names violate (good) Microsoft naming conventions. It does not tell anything by itself adding a delegate to an invocation list is done using += event operator. This is just suggested by the name of the method, as it looks like an auto-generated code you got from the designer. You did provide any evidence that MainForm_KeyUp is an event handler added to the invocation list of the KeyUp event instance of any of the forms. It is not clear what is it and why "Frm2 comes to MainForm_KeyUp". If you call .ShowDialog, your form is shown in the modal state, so the user is supposed to work only with this form in this application (so, it is not system-modal, you can switch to other applications, activate their windows), cannot invoke events of other forms. First of all, none of the forms are children here, and your main form is not a parent of your other form.
