C#表单键跟踪 [英] C# form key tracking

查看:97
本文介绍了C#表单键跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须跟踪是否在表单上按下了F2键,然后执行一些操作;我已经编写了以下代码,但似乎什么也没发生

代码:

I have to track if F2 key is pressed on the form and perform some action after that ; I have written following code but nothing seems to happen

Code:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
      {

          if (e.KeyChar == (char)Keys.F2)
          {
              MessageBox.Show("F2 pressed");
              //do something
          }
      }



请提出可能的解决方案.....?



Please suggest possible solution.....?

推荐答案

您可能需要实现键盘挂钩:
在C#中处理全局鼠标和键盘挂钩 [
You will probably need to implement a keyboard hook:
Processing Global Mouse and Keyboard Hooks in C#[^]

or attach the handler to every control on your form. I would prefer the former.

Best regards
Espen Harlinn


非字符键不会引发KeyPress事件,而检测诸如F2之类的键的唯一方法是通过KeyDown和Keyup事件
The KeyPress event is not raised by non character keys and the only way to detect keys such as F2 is by the KeyDown and Keyup events msdn[^].

You''ll also need to ensure that the KeyPreview property of the form is true.

Alan.


refer this link

<a href="http://www.java2s.com/Code/CSharp/GUI-Windows-Form/FormKeyPressaction.htm">http://www.java2s.com/Code/CSharp/GUI-Windows-Form/FormKeyPressaction.htm</a>[<a href="http://www.java2s.com/Code/CSharp/GUI-Windows-Form/FormKeyPressaction.htm" target="_blank" title="New Win


这篇关于C#表单键跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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