功能键不触发 [英] Function keys not firing

查看:84
本文介绍了功能键不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在尝试从代码中触发ESC,F1和F2键,只有"ESC"被触发而不是F1和F2.以下代码是否有错误?

Dear all,

I am trying to fire ESC,F1 and F2 keys from code, Only "ESC" is getting fired not F1 and F2. Is there any mistake in the following code?

If e.KeyChar = Microsoft.VisualBasic.ChrW(27) Then
       Panel7.Visible = True
       Panel7.Location = New Point(1, 1)
       txtsub.Focus()
End If
If e.KeyChar = Microsoft.VisualBasic.ChrW(112) Then
       Panel8.Visible = True
       Panel8.Location = New Point(1, 1)
       txtsub.Focus()
End If
If e.KeyChar = Microsoft.VisualBasic.ChrW(113) Then
       Panel9.Visible = True
       Panel9.Location = New Point(1, 1)
       txtsub.Focus()
End If

推荐答案

您的问题是您已通过KeyPress事件捕获了此问题.

遇到问题时,您需要在代码上查找文档.来自MSDN:

非字符键不会引发KeyPress事件;但是,非字符键会引发KeyDown和KeyUp事件."

因此,您需要使用KeyUp事件而不是KeyPress事件.另外,KeyUp事件更具可读性,并提供更多选项.例如,不用写

Your problem is that you''ve trapped this through the KeyPress event.

You need to look up the documentation on the code when you''re having an issue. From MSDN:

"The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events."

So, you need to use the KeyUp event instead of the KeyPress event. Also, the KeyUp event is more readable and provides more options. For example, instead of writing

If e.KeyChar = Chr(112) Then



你可以写



you can write

If e.KeyCode = Keys.F1 Then



调试您的代码的人阅读第二个代码要容易得多.

另外,作为语法注释,当您说要从代码发射"键时,这意味着您要尝试告诉计算机该键已被按下.您将要捕获"该键.



It''s a lot easier for someone debugging your code to read the second.

Also, as a grammatical note, when you say that you''re trying to "fire" a key from code, it means that you''re trying to tell the computer that that key was pressed. You''re tying to "trap" the key.


对于您的新问题(从我上一个答案留下的评论中),用户应始终按ctrl,shift或请先按Alt.这就是它随处可见的方式.

如果我使用的是Word,请突出显示一些文本,然后按住"c",然后按ctrl键启动复制事件,它将无法正常工作.将会发生的情况是,突出显示的文本将首先变为字母"c",然后将重复"c",直到按下控件为止.届时,将不会突出显示任何文本,因此不会复制任何内容.

应始终假定应先按ctrl,alt和shift.

但是,如果先按Ctrl再按F1,并且收到重复的消息,则问题出在代码中.假设您写了
As far as your new question (from the comment left on my last answer), the user should always press ctrl, shift, or alt first. That''s the way it works everywhere.

If I am in Word, highlight some text, and hold "c" and then hit ctrl to initiate a copy event, it won''t work. What will happen is that the highlighted text will first become the letter "c", then "c" will be repeated until control is pressed. At that point, no text will be highlighted, so nothing will be copied.

It should always be assumed that ctrl, alt, and shift should be pressed first.

If, however, you are pressing Ctrl first and then F1 and you are getting duplicate messages, the problem is in your code. Let''s say, you have written
If e.KeyCode = Keys.F1 Then
  MessageBox.Show("F1")
End If
If e.KeyCode = Keys.F1 and e.Control Then
  MessageBox.Show("F1 + Ctrl")
End If


您将收到两条消息.你知道为什么吗?

另外,由于您似乎是.Net的新手,因此仅提供一些小技巧.

首先,不应再使用MsgBox.那是旧的VB6结构.您应该使用MessageBox.Show("some text").

其次,您不需要针对布尔值的true或false进行测试.那只是应用程序必须采取的附加步骤.


you will get two messages. Do you see why?

Also, just a few little tips since you appear to be new to .Net.

First, MsgBox shouldn''t be used anymore. That''s an old VB6 structure. You should be using MessageBox.Show("some text").

Secondly, you don''t need to test boolean values against true or false. That is just added steps the application will have to take.

If e.Control Then
'...
'
'is the same as
If e.Control = True


区别在于e.Control = True必须首先测试该条件,然后检查该测试返回的是true还是false.

最后,如果您正在测试e.KeyCode的许多可能性,则最好使用Select Case结构.它看起来像:


The difference is that e.Control = True has to first test that condition and then check whether that test returned true or false.

Finally, if you are testing a lot of possibilities for e.KeyCode, you would be better off using the Select Case structure. It would look like:

Dim output as String = ""

Select Case e.KeyCode
  Case Keys.F1
    output = "F1"
  Case Keys.F2
    output = "F2"
End Select

If e.Control Then
  output = output & "+Ctrl"
End If

MessageBox.Show(output)


这篇关于功能键不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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