KeyDown :识别多个键

发布于 2024-08-01 20:01:34 字数 339 浏览 4 评论 0原文

如何在 KeyDown 中确定 CtrlUp 已被按下。

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Up)
    {
        //do stuff
    }
}    

无法工作,因为两个键永远不会在同一秒内完全相同地按下。 你总是先按Ctrl,然后再按另一个......

How can I determine in KeyDown that CtrlUp was pressed.

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Up)
    {
        //do stuff
    }
}    

can't work, because never both keys are pressed exactly in the same second. You always to at first the Ctrl and then the other one...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

極樂鬼 2024-08-08 20:01:34

您可以使用 ModifierKeys 属性:

if (e.KeyCode == Keys.Up && (ModifierKeys & Keys.Control) == Keys.Control)
{
    // CTRL + UP was pressed
}

请注意,ModifierKeys 值可以是值的组合,因此如果您想要检测是否按下了 CTRL,无论 SHIFT 的状态如何或 ALT 键,您将需要执行按位比较,如我上面的示例所示。 如果您想确保没有按下其他修饰符,您应该检查是否相等:

if (e.KeyCode == Keys.Up && ModifierKeys == Keys.Control)
{
    // CTRL + UP was pressed
}

You can use the ModifierKeys property:

if (e.KeyCode == Keys.Up && (ModifierKeys & Keys.Control) == Keys.Control)
{
    // CTRL + UP was pressed
}

Note that the ModifierKeys value can be a combination of values, so if you want to detect that CTRL was pressed regardless of the state of the SHIFT or ALT keys, you will need to perform a bitwise comparison as in my sample above. If you want to ensure that no other modifiers were pressed, you should instead check for equality:

if (e.KeyCode == Keys.Up && ModifierKeys == Keys.Control)
{
    // CTRL + UP was pressed
}
对你的占有欲 2024-08-08 20:01:34

这肯定会起作用。 请小心处理 KeyUp 事件而不是 keyDown

private void mainForm_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
        {
             //insert here
        }
    }

对我来说,keyDown 不起作用,keyUp 对于相同的代码起作用。

我不知道为什么,但似乎是因为 keyDown 事件在您按下任意键后立即发生,即使这是 ctrl 键,所以如果您按下 ctrl +Up 您将在按 UP 键之前按 ctrl 键,因此该事件将在您按下另一个键之前发生,也按第二个键将再次触发该事件。

而使用KeyUp时,只有松开按键才会触发该事件,因此您可以按ctrl,然后再按第二个键,这将触发一个事件。

this will work for sure. Be careful to handle KeyUp event and not keyDown.

private void mainForm_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
        {
             //insert here
        }
    }

For me, keyDown didn't work, keyUp worked instead for the same code.

I don't know why, but it seems because keyDown event happens directly after you press any key, even if that was ctrl key, so if you pressed ctrl+Up you will press ctrl key before the UP key and thus the event will occur before you can press the other, also pressing the second key will triggers the event again.

While using KeyUp will not trigger the event until you release the key, so you can press ctrl, and the press the second key, which will trigger one event.

旧时模样 2024-08-08 20:01:34

你必须记住按下的键(即在布尔数组中)。 并在按下(keydown)时将位置设置为 1,在 up 时将位置设置为 0。

这样您就可以跟踪多个密钥。 我建议只为特殊键创建一个数组,

这样你就可以这样做:

 if (e.KeyCode == Keys.Control)
 {
        keys[0] = true;
 }
// could do the same with alt/shift/... - or just rename keys[0] to ctrlPressed

if (keys[0] == true && e.KeyCode == Keys.Up)
 doyourstuff

you have to remember the pressed keys (ie in a bool array). and set the position to 1 when its pressed (keydown) and 0 when up .

this way you can track more than one key. I suggest doing an array for special keys only

so you can do:

 if (e.KeyCode == Keys.Control)
 {
        keys[0] = true;
 }
// could do the same with alt/shift/... - or just rename keys[0] to ctrlPressed

if (keys[0] == true && e.KeyCode == Keys.Up)
 doyourstuff
过期以后 2024-08-08 20:01:34

您可以像这样检查 KeyEventArgs 的修饰符:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
    {
        //do stuff
    }
}  

MSDN 参考

You can check the modifiers of the KeyEventArgs like so:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
    {
        //do stuff
    }
}  

MSDN reference

伪装你 2024-08-08 20:01:34

KeyEventArgs 上的 MSDN 页面

if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
    //Do stuff...
}

From the MSDN page on KeyEventArgs:

if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
    //Do stuff...
}
熊抱啵儿 2024-08-08 20:01:34
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Up && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
         //do stuff
    }
}

仅当您先按 LeftCtrl,然后按“UP”时,此代码才会起作用。
如果顺序不重要,我建议这样做:

if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))&& Keyboard.IsKeyDown(Key.Z))
{
    //do stuff
}

在这种情况下,两个 Ctrl 都会被考虑在内,并且顺序不重要。

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Up && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
         //do stuff
    }
}

This code will work only if you press first LeftCtrl, then "UP".
If order has no importance, I recommend that one :

if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))&& Keyboard.IsKeyDown(Key.Z))
{
    //do stuff
}

In that case, both Ctrl are taken in account, and no importance about the order.

吲‖鸣 2024-08-08 20:01:34

KeyEventArgs 中,有属性 CtrlAltShift 显示这些按钮是否被按下。

In the KeyEventArgs there are properties Ctrl, Alt and Shift that shows if these buttons are pressed.

菊凝晚露 2024-08-08 20:01:34

您可以尝试使用 Keyboard 对象来检测 IsKeyDown 属性。 另外,如果您不希望覆盖浏览器快捷方式,可以将 Handled 属性设置为 true。但是在覆盖浏览器快捷方式时要小心,因为它可能会导致混乱。

private void Page_KeyDown(object sender, KeyEventArgs e)
{
    // If leftCtrl + T is pressed autofill username
    if (Keyboard.IsKeyDown(Key.T) && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        txtUser.Text = "My AutoFilled UserName";
        e.Handled = true;
    }
}

You can try using the Keyboard object to detect the IsKeyDown property. Also, if you don't want the browser shortcut to over-ride you can set Handled property to true.But be careful when over-riding browser shortcuts as it could cause confusion.

private void Page_KeyDown(object sender, KeyEventArgs e)
{
    // If leftCtrl + T is pressed autofill username
    if (Keyboard.IsKeyDown(Key.T) && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        txtUser.Text = "My AutoFilled UserName";
        e.Handled = true;
    }
}
放肆 2024-08-08 20:01:34

您可以尝试我的工作代码:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        if(e.Alt==true){
            //do your stuff
        }
    }
}

我使用此代码是因为我不知道为什么当我使用时:

(e.Keycode == Keys>up && e.Alt==true)

不起作用。

you can try my working code :

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        if(e.Alt==true){
            //do your stuff
        }
    }
}

i use this code because i don't know why when i use :

(e.Keycode == Keys>up && e.Alt==true)

didn't work.

故事灯 2024-08-08 20:01:34

if (e.Control && e.Shift && e.KeyCode == Keys.A)
{

}

if (e.Control && e.Shift && e.KeyCode == Keys.A)
{

}

挽心 2024-08-08 20:01:34

KeyDown 事件将仅保存最近按下的键的信息。 我已经成功构建了一个字符串,其中包含所有最新的按键连接在一起。

如果单击“Control”键,或者字符串长度超过 10 个字符,我会清除该字符串。

每次检查字符串,然后执行任务,这对于应用程序中的秘密热键功能非常有用。

    private void ConfigurationManager_KeyDown(object sender, KeyEventArgs e)
    {
        string currentKey = e.KeyCode.ToString().ToLower();

        if ((currentKey == "controlkey") || (hotKeyList.Length > 10))
        {
            hotKeyList = "";
        }
        else
        {
            hotKeyList += currentKey;
        }

        if ((hotKeyList == "int") && (!adminLogin))
        {
            hotKeyList = "";
            adminLogin = true;
            AdminLoginEvn();
        }
    }

对于上面的函数,我的热键是“Control(清除字符串)+ i + n + t”来触发我的 AdminLoginEvn 方法。 adminLogin 布尔值已合并,因此我只在应用程序打开时运行 AdminLoginEvn 一次。

The KeyDown event will only hold the information for the most recent key that was pressed. I have had success building a string that contains all the most recent key down keys concatenated together.

If a key down of "Control" was clicked, or the strings becomes greater than 10 chars long, I clear the string.

Checking the string each time, then performing a task afterwords gives a great use for a secret hotkey function within your application.

    private void ConfigurationManager_KeyDown(object sender, KeyEventArgs e)
    {
        string currentKey = e.KeyCode.ToString().ToLower();

        if ((currentKey == "controlkey") || (hotKeyList.Length > 10))
        {
            hotKeyList = "";
        }
        else
        {
            hotKeyList += currentKey;
        }

        if ((hotKeyList == "int") && (!adminLogin))
        {
            hotKeyList = "";
            adminLogin = true;
            AdminLoginEvn();
        }
    }

For the function above my hotkeys would be "Control (clears the string) + i + n + t" to fire my AdminLoginEvn method. The adminLogin boolean was incorporated so I only run my AdminLoginEvn one time while the application is open.

混吃等死 2024-08-08 20:01:34

我测试了下面的代码。 有用...

private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((int) e.KeyData == (int) Keys.Control + (int) Keys.Up)
        {
            MessageBox.Show("Ctrl + Up pressed...");
        }
    }

I tested below code. it works...

private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((int) e.KeyData == (int) Keys.Control + (int) Keys.Up)
        {
            MessageBox.Show("Ctrl + Up pressed...");
        }
    }
七月上 2024-08-08 20:01:34

我花了一段时间才找到在尝试检测 [Alt][Right] 时最终需要的提示。 我在这里找到它:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355ab9a-9214-4fe1-87ea-b32dfc22946c/issue-with -alt-key-and-key-down-event?forum=wpf

它归结为我使用的 Shortcut 辅助类中的类似内容:

public Shortcut(KeyEventArgs e) : this(e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key, Keyboard.Modifiers, false) { }

public Shortcut(Key key, ModifierKeys modifiers, bool createDisplayString)
{
    ...
} 

在“重新映射”原始内容之后值(注意 e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key 部分),进一步处理可以照常进行。

It took me a while to find the hint I ultimately needed when trying to detect [Alt][Right]. I found it here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355ab9a-9214-4fe1-87ea-b32dfc22946c/issue-with-alt-key-and-key-down-event?forum=wpf

It boils down to something like this in a Shortcut helper class I use:

public Shortcut(KeyEventArgs e) : this(e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key, Keyboard.Modifiers, false) { }

public Shortcut(Key key, ModifierKeys modifiers, bool createDisplayString)
{
    ...
} 

After "re-mapping" the original values (notice the e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key part), further processing can go on as usual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文