本文介绍了查找用户是否按下某个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询用户是否按下了键盘键.我尝试了一些代码:

I want to look-up that whether a user's pressed a keyboard key.I tried some code:

    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(System.Int32 vKey);
    .....
     if ((GetAsyncKeyState(Keys.F10) == -32767))
       {....}

它所做的只是检查 F10 键的按下情况.它不关心是 Shift+F10 还是 Ctrl+F10 或 F10.但我想要的是分别查找它们,如果是 Shift+F10,则告诉我用户按下了 Shift+F10,如果是 F10,则告诉我用户按下了 F10.我怎样才能以简单的方式到达那里?

What it does is that it just check the pressing of F10 key.It doesn't care whether it is Shift+F10 or Ctrl+F10 or F10.But what i wanted is that to look-up them separately say if it is Shift+F10 then tell me the user pressed Shift+F10,if it is F10 then tell me the user pressed F10.How can i get there a simple way?

推荐答案

您需要为您感兴趣的每个键检查 GetAsyncKeyState.在这种情况下,您将检查 F10,然后检查shift、ctrl、alt 等.请参阅虚拟密钥代码 有关可能的值,请参阅 获取AsyncKeyState.

You need to check GetAsyncKeyState for each key you're interested in. In this case you will check for F10 and then check for shift, ctrl, alt and others. See virtual key codes for the possible values and see GetAsyncKeyState.

short key = GetAsyncKeyState(Keys.F10);
// Check the most significant bit to see if the key is down
if ( ( key & 0x8000 ) > 0 )
{
     // Check  shift is down 0x10 is value for VK_SHIFT
     key = GetAsyncKeyState( 0x10 );
     if ( key & 0x8000 ) > 0 )
     {
         // Shift key is down as well
     }
     // Repeat for other key states.
}

看c#keys enumeration 有许多可能的移位值,您可能需要尝试一下.

Looking at the c# keys enumeration there are number of possible values for shift, you might need to experiment a bit.

这篇关于查找用户是否按下某个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:20