更新时间:2023-02-22 14:36:30
中捕获所有键盘键,包括键左键,键右键,键盘键事件一旦我发现有些情况我在哪里无法使用该方法捕获击键。这篇文章中描述的方法帮助我和mey在某些情况下有用:http://social.msdn.microsoft.com/Forums/vstudio/en-US/cf884a91-c135-447d-b16b- 214d2d9e9972 / capture-all-keyboard-input-what-what-control-has-focus [ ^ ]
Once I found there were some situations where I was not able to capture keystrokes with that method. The method described in this post helped me and mey be useful in some cases: http://social.msdn.microsoft.com/Forums/vstudio/en-US/cf884a91-c135-447d-b16b-214d2d9e9972/capture-all-keyboard-input-regardless-of-what-control-has-focus[^]
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown);
textBox1.PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown);
}
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
try
{
MessageBox.Show(e.KeyCode.ToString() + " key pressed on " + sender.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
使用任何控件的keydown事件..
这里我用的是textbox' x KeyDown事件..
use keydown event of any control ..
here i am used textbox'x KeyDown event..
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyData.ToString());
}