简体   繁体   中英

key combination c# other than hotkeys

I am implementing shortcut key combination logic like

Ctrl + A

Ctrl + Alt + D

but now I am looking for a trick to implement keyboard key combination without hotkey combination like : A + D but not getting any right things.

So please help to implement it.

Updated

Currently I am not able to add key combination other than hotkey combination.because whenever I am pressing two keys like : **A + D** it returns only single keys ascii so I am not getting two keys combination.

I have used following stuff http://www.codeproject.com/Articles/442285/Global-Shortcuts-in-WinForms-and-WPF

In the past I used the GetKeyState Api (inside a similar class for catching system wide shortcuts). Perhaps new variants are available nowadays (WPF has a KeyBoard.IsKeyDown I believe), but this should still work:

    [DllImport("user32", CharSet = CharSet.Auto,
    ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
    static extern short GetKeyState(int keyCode);

    public static bool CheckKeysPressed(params Keys[] keys)
    {
        for (int i = 0; i < keys.Length; i++)
            if (!CheckKeyPressed((int)keys[i])) return false;
        return true;
    }
    public static bool CheckKeyPressed(Keys key)
    {
        return CheckKeyPressed((int)key);
    }
    public static bool CheckKeyPressed(int vkey)
    {
        short ks = GetKeyState(vkey);
        return ks == 1;
    }

Usage: if(CheckKeysPressed(Keys.A, Keys.D)) {/*...*/}

Declare 2 bool variable (say IsADown and IsDDown ) and " true " them on events of both A and D . Also detect on both events if other bool is down or not. If it is, then do what you have to do with both button pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM