简体   繁体   中英

C# Key hooks key down event

Creating a key hook so that when the combination is pressed, the application will open again. I have looked into various ways of doing it, however I do not know what the input combination will be unlike this example:

if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control && e.Modifiers == Keys.Shift)
 {
//Do work
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
//Paste
}

Our input combination is from the user, where they select which combination they want to input from a combo box.

public void KeyboardHooks_OnKeyPress(object sender, KeyPressArgs e)
    {
        //The first input
        if (LastKey != Keys.None)
        {
            Keys combinationOne = (Keys)cmbCombinationOne.SelectedValue;
            Keys combinationTwo = (Keys)cmbCombinationTwo.SelectedValue;
        }
        LastKey = e.Key;
        MessageBox.Show("KeyPressed");
    }

Not sure on how to go about setting our values to the combo box's

From your code snippets it looks like you're going the route of WinForms key events. If the user does a key combination in your application and you do this 'open' of something else, you're on the right path. You'll just need to make it dynamic to see if the user-defined items are pressed. So when you save the user settings, convert it to a keycode so you can do the generic

if(e.KeyCode == Settings.FirstModKey && e.KeyCode == Settings.SecondModKey && e.KeyCode == Settings.FirstKey)

You'll need to consider the multiple scenarios, the modifiers Shift, Alt and Control could be none, one, two, or all three. In my above, you could have FirstModKey and SecondModKey the same value if the user chose Ctrl only, or they could be handling if they did Ctrl and Shift both. Then FirstKey is the non-mod key, like 'A'.

the application will open again

However, from the quote it sounds like you want a global hook, that wherever the user is in any application, with yours not running, you want to listen and do work if its your keycode. You need to look into a service and low level hooks. This can come close to keylogging and you need to be careful who your audience is, security risks and concerns that you might be breaking compliance.

    {
        //The first input
        if (LastKey != Keys.None)
        {
            int combination1 = (int)Enum.Parse(typeof(Keys), cmbCombinationOne.SelectedValue.ToString());
            int combination2 = (int)Enum.Parse(typeof(Keys), cmbCombinationTwo.SelectedValue.ToString());

            int LastKeyPress = (int)Enum.Parse(typeof(Keys), LastKey.ToString());


            ThisKey = e.Key;

            if (combination1 == LastKeyPress && combination2 == Convert.ToInt32(ThisKey))
            {
                MessageBox.Show("Key pressed");
            }
        }
        LastKey = e.Key;
    }

This worked with my original existing code

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