简体   繁体   English

C#键挂钩键按下事件

[英]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. 从您的代码段中,您似乎正在走WinForms关键事件的路线。 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. 您需要考虑多种情​​况,修饰符Shift,Alt和Control可以为无,一,二或全三。 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. 在我上面的内容中,如果用户仅选择Ctrl,则FirstModKey和SecondModKey可以具有相同的值,或者如果用户同时选择Ctrl和Shift,则它们可以进行处理。 Then FirstKey is the non-mod key, like 'A'. 然后FirstKey是非mod键,例如'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 这与我原来的原始代码一起工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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