Shortcut Key Using C #

I want to create a simple application that has send keys (like keyboard shortcuts). The fact is that whenever the created application is an inactive window, the system still recognizes the pressed user keys while the system is running.

In short, it's just like clicking (window + D), which immediately shows your desktop when you are in which application / window.

Can anyone help me on how I can do this in C # 2005

+5
source share
2 answers
+2
source

Use the following:

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

and

[Flags]
public enum ModifierKeys : uint
{
    Alt = 1,
    Control = 2,
    Shift = 4,
    Win = 8
}

private ModifierKeys _getModifierKeys(bool isAlt, bool isCtrl, bool isShift, bool isWin)
{
    return (isAlt ? ModifierKeys.Alt : 0) |
            (isCtrl ? ModifierKeys.Control : 0) |
            (isShift ? ModifierKeys.Shift : 0) |
            (isWin ? ModifierKeys.Win : 0);
}

, ,

RegisterHotKey(hWndNotify, id,
                    (uint)_getModifierKeys(_isAlt, _isCtrl, _isShift, _isWin),
                    (uint)_key);

hWnd, WM_HOTKEYREADY,

public bool Matches(ref Message m)
{
    Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
    ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);

    if ((key == Key) &&
        (modifier == Modifier))
    {
        return true;
    }

    return false;
}
+5

All Articles