如何不允许一次按键收到多个按键?

how not to allow multiple keystokes received at one key press?(如何不允许一次按键收到多个按键?)
本文介绍了如何不允许一次按键收到多个按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们按下一个键并持续按下它时,keypress 和 keydown 事件会不断触发.有没有办法让这些只在一个完整的循环后触发,例如按键然后按键.

when we press a key and keep pressing it the keypress and keydown event continuously fires. Is there a way to let these fire only after a complete cycle ,eg keydown and then key up.

我希望用户不能连续按键,而是希望用户必须按下然后释放键盘才能输入字符!

I would like the user not to be able press the key continuously rather would like the user have to press then release the key board to type a character !

这样就不会发生以下情况,例如:当用户按下 'p' 1 秒时,pppppppppppppppppppppppp.

so that following case do not occur eg : pppppppppppppppppppppppp when user presses 'p' for 1 sec.

推荐答案

声明一个boolean isKeyDown,在KeyDown事件中,设置boolean为true.在 KeyUp 事件中,将布尔值设置为 false.

Declare a boolean isKeyDown, in theKeyDown Event, set the boolean to true. In the KeyUp event, set the boolean to false.

这是示例代码

bool isKeyDown=false;
public void control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if(isKeyDown)
      return;
    isKeyDown=true;
    // do what you want to do
}

public void control1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
   isKeyDown=false;
   // do you key up event, if any. 
}

这篇关于如何不允许一次按键收到多个按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)