如何检测按键被按下并处理? [英] How to detect a key is pressed and handled ?

查看:104
本文介绍了如何检测按键被按下并处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是这个论坛的新手,我控制一个键有问题。



我想解决的问题是:



1)假设我们在c#应用程序中有一个表单并有一个标签。



2)I想使用箭头键来改变标签。但我想要的重要的是,如果我按下键并处理它,标签必须显示指定的文本,而我一直按下键。如果我按下并立即将其取回,标签文本必须返回其原始状态。



3)这只是一个问题的证明。我想在其他应用程序中使用此属性,在此之前,我必须知道并且可以正确使用该属性。



提前感谢..



我尝试过:



我试图使用KeyDown,KeyPress和KeyUp事件,但我不能采取我想要的。一旦我按下键并保持该状态,情况就是一样。但是,我按下后退,标签不会改变(如果我不按键,我希望标签返回其原始状态)。

Hello everyone, I am new at this forum and i have a problem by controlling a key.

The problem that i want to solve is that way :

1) Let's say we have a form in c# application and have a label.

2) I want to use arrow keys to change tha label. But the important thing that i want is,if i press the key and handle it, the label must show the specified text while i keep pressing the key. If i press and take it back immidiately, the label text must return its original state.

3) This is just a demonstration of a problem. I want to use this property in other application , before doing that, i have to know and can use the property in proper way.

Thanks in advance..

What I have tried:

I tried to use KeyDown, KeyPress and KeyUp events but i couldn't take what i want. Once i press the key and keep it that state, the situation is same.However, i take the pressing back ,the label does not change (I want the label to return its original state if i don't keep pressing the key).

推荐答案

对于光标键,你没有得到一个KeyPress事件 - 你做的是alpha键等等 - 你得到的是KeyDown事件,然后是短暂延迟(在OS键盘设置中设置),然后是一系列更多KeyDown事件,直到您释放该键(也在OS键盘设置中设置间隔),此时您将获得KeyUp事件。

这称为自动重复,初始延迟通常约为半秒左右,然后按键重复每秒约8到10个键。

如果你想做一些不同的事情 long press然后使用类级别int,最初为零,并在每次获取特定键的KeyDown事件时为其添加一个。在KeyUp事件中,检查它 - 如果它是一个,还原你的标签/发送取消到Arduino,然后将其重置为零。



你必须添加一些东西来检测已更改的密钥,等等,但这是一般的想法。

如果您在表单级别处理这些,则需要将Form.KeyPreview属性设置为true以获取事件。
For a cursor key, you don't get a KeyPress Event - you do for alpha keys and so forth - what you get is a KeyDown event, then a short delay (set in the OS keyboard settings) and then a sequence of more KeyDown events until you release the key (interval also set in the OS keyboard settings), at which point you get a KeyUp event.
This is called "autorepeat" and the initial delay is generally around half a second or so, then the key repeats at around 8 to 10 keys per second.
If you want to do something different for "long presses" then use a class level int, initially zero, and add one to it each time you get the KeyDown event for a particular key. In the KeyUp event, check it - if it's one, revert your label / send a cancel to the Arduino, then reset it to zero.

You'll have to add some stuff to detect changed keys, and so forth, but that's the general idea.
If you are handling these at the form level, you need to set the Form.KeyPreview property to true to get the events at all.


我认为这里有一个更简单的解决方案,如果这是你正在使用的WindowsForms。



NumericUpDown控件...当它具有Focus时,其'InterceptArrowKeys属性设置为'true ...将自动响应向上或向下,递增或递减的箭头键,显示的值基于你如何设置Control的'Minimum,'Maximum和'Increment属性的初始状态。



如果你需要其他行为(比如对左,右做出反应) ,PageUp,PageDown,End,Home键)你可以n子类NumericUpDown并覆盖KeyPreviw,ProcessCmdKeys等
I think there is a much simpler solution to your design goal here, if this is WindowsForms you are working with.

The NumericUpDown Control ... when it has Focus, and its 'InterceptArrowKeys Property is set to 'true ... will automatically respond to the arrow-keys being held up or down, incrementing, or decrementing, the shown value based on how you set the initial state of the Control's 'Minimum, 'Maximum, and 'Increment properties.

If you need other behavior (like reacting to Left, Right, PageUp, PageDown, End, Home keys) you can sub-class the NumericUpDown and override KeyPreviw, ProcessCmdKeys, etc.


首先,非常感谢您的意见和建议。我解决了这个问题。我的错是我不知道KeyDown和KeyUp事件的功能。我将在KeyDown事件中使用我的条件,当我使用它时,它表示我想要的状态。而KeyUp事件用于检查指定的键是否仍然按下。再次感谢..
First of all, thanks a lot for your comments and advices. I solve the problem. My fault is that i dont know the functionality of KeyDown and KeyUp events. I will use my conditions in KeyDown event and while i am using it ,it says the state that i want.And KeyUp event is for the checking the specified key if it is still pressing or not. Thanks a lot again..


这篇关于如何检测按键被按下并处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆