Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut Key using C#

I want to create a simple application that having a send keys (like a shortcut keys). The case is, whenever the created application is inactive window, The system still recognize the pressed keys of the user as long as the system is running.

In short, It is simply like pressing (window + D) that show your desktop immediately whenever on what application/window are you in.

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

like image 263
Bryan Avatar asked Jun 02 '11 05:06

Bryan


People also ask

How do you use keyboard shortcuts?

To use a keyboard shortcut, you'll need to press one or more keys simultaneously. Many Windows shortcuts that involve the Ctrl key are similar on a Mac, except on a Mac, Ctrl is often replaced with Command. Keyboard shortcuts may have different behaviors in different apps. Understand how key shortcuts work.

How do you type the ç symbol on a keyboard?

To use this shortcut, simultaneously press and release the Control and the Comma keys [Ctrl] + [ , ], then hit the [C] key once. Alternatively, just type 00E7 and then press Alt + X to get the Ç sign. The table below contains everything you need to know to be able to type this symbol on both Windows and Mac.

What are the keyboard shortcuts for the Windows logo?

Windows logo key keyboard shortcuts Press this key To do this Windows logo key + J Set focus to a Windows tip when one is a ... Windows logo key + K Open the Connect quick action. Windows logo key + L Lock your PC or switch accounts. Windows logo key + M Minimize all windows. 33 more rows ...

How do you list keyboard shortcuts on a resume?

The de facto standard for listing a shortcut is listing the modifier key, a plus symbol, and another key. In other words, "Ctrl+S" is telling you to press and hold the Ctrl key, and then press the S key too. You can also find the shortcut keys in popular programs by looking for underlined letters in their menus.


1 Answers

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);
}

then, to register your hotkey,

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

in the host hWnd, listen for WM_HOTKEYREADY, then check for matches to your hotkey using

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;
}
like image 91
tofutim Avatar answered Sep 17 '22 15:09

tofutim