具有多个目的的键盘键 [英] Having a keyboard key that has multiple purposes
问题描述
我想使用键 SHIFT + D 组合键,因此当同时按下两个键时,目的X就会启动.但是我的键 D 也被用于启动目标Y.如何确定当我按下键 SHIFT + D 时,目的X,只有目的X已启动,而不是目的Y.
I want to make a key combination with Keys SHIFT + D hence when both keys are pressed, purpose X starts. But my Key D is also being used to start purpose Y. How do I make sure when I'm pressing the key SHIFT + D, purpose X and only Purpose X is started and not Purpose Y.
FYI ---将在D之前按Shift.
FYI --- Shift will be pressed before D.
我尝试自己解决此问题,这是我的代码...
I tried solving this problem myself, here is my code...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == moveRight && isJumping != true && isHovering != true)
{
if (Shft_KeyPressed == true)// This block was suppose to fix my problem
{
canFly = true; // method for Purpose X will then start if this is true
} else
{
facingDirection = 1;
standTimer.Stop();
Walking_Animator(); // This is Purpose Y
}
} else if (e.Shift)
{
Shft_KeyPressed = true;
Flying_Animator(); // this is Purpose X
}
}
推荐答案
要检查是否按下 D 或按下 Shift + D 您可以依靠 KeyData
属性.
To check if D is pressed or Shift + D is pressed you can rely on KeyData
property of event argument.
KeyData
A Keys,代表所按下键的键控代码, 与修饰符标志结合使用,这些修饰符标志指示CTRL的组合, SHIFT,并同时按下ALT键.
KeyData
A Keys representing the key code for the key that was pressed, combined with modifier flags that indicate which combination of CTRL, SHIFT, and ALT keys was pressed at the same time.
if (e.KeyData == Keys.D)
{
// Just D is pressed
}
if (e.KeyData == (Keys.D | Keys.Shift))
{
// Exactly Shift + D is pressed
}
注释1:您还可以使用Shift
属性来检查是否按下了 Shift 键.但是请记住,如果Shift
为true,并不意味着 Shift 是唯一按下的修饰键.但是我检查过的上述条件可以保证按下的组合键是 Shift + D .
Note 1: You can also use Shift
property to check if Shift key is pressed. But keep in mind if Shift
is true, it doesn't mean Shift is the only pressed modifiers key. But above criteria which I checked guarantees that the pressed combination is Shift + D.
注2::如果使用KeyDown
事件,即使窗体的其他控件包含焦点,也要接收键,则应将KeyPreview
设置为true
.另外,您也可以不设置KeyPreview
而覆盖ProcessCmdKey
.您也可以访问ProcessCmdKey
中的关键数据组合.
Note 2: If you are using KeyDown
event, to receive the keys even when other controls of the form contain focus, you should set KeyPreview
to true
. Also as another option you can override ProcessCmdKey
without setting KeyPreview
. You have access to key data combination in ProcessCmdKey
as well.
这篇关于具有多个目的的键盘键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!