Soubarna Das wrote:
I solved out everything by following your advice. I have a question on that topic. Is it possible to add short-cut keys (Key sequence) for a particular keyboard programmatically via VB without going to control panel? Generally we can set key sequences by going to Control Panel -> Region And Languages -> Change Keyboards -> Advanced Key Settings -> Change Key Sequence.
…
Can you explain the both per-application and via RegistryHotkey.
Sure. But this is a separate question, not much related to your original issues.
Let's start with application level. This is simple.
First of all, you need to understand that keys are processed in several stages. Keyboard "knows nothing" about characters or virtual keys, it produces scan codes. If you handle something like
KeyUp
or
KeyDown
, you deal with
virtual keys. The event come to your application before something is interpreted as characters, that is, before keyboard layout.
So, let's say, you have to handle if Ctrl+Shift+O is dispatched to some form. It will happen if you press on the key where the Latin O is written, even if the character is different for some alternative keyboard layout. In other words, even if in the current keyboard layout, pressing this key produces Greek Ω, you still need to check up for the virtual key
System.Windows.Forms.Keys.O
.
If you need it application-wide, you can do it for all forms. I can be something like:
Public Partial Class MainForm
Private Sub Setup()
Me.KeyPreview = True
Me.KeyDown += Function(sender, eventArgs)
If eventArgs.Modifiers =
(System.Windows.Forms.Keys.Control
Or System.Windows.Forms.Keys.Shift) AndAlso
eventArgs.KeyCode = System.Windows.Forms.Keys.O Then
ActivateSomething
End If
End Function
End Sub
End Class
In this code sample, your form will set up with this processing if you call
Setup
somewhere from the constructor. Note that
KeyPreview
is important because the keyboard focus is usually taken by some control, and you don't want this control to interfere with your hot key; the hot key should always be handled.
Same story goes with
shortcut properties, such as, say,
ToolStripMenuItem.ShortcutKeys
. If you set Ctrl+Shift+O, it will be the key corresponding to Latin O, not the character according to keyboard layout. You can use menu items as application shortcuts.
In contrast to that, if you prefix, say, menu or button text with '&', next key will be underscored and work as accelerator. In this technique, you need to use Alt+O, where O should be real Latin O. The change of keyboard layout will disable this key, if it's not there in the keyboard layout.
That's all about application-local handling. Next step will be about the system-global.
—SA