csharp 在实际控制之前捕捉击键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 在实际控制之前捕捉击键相关的知识,希望对你有一定的参考价值。

<span style="color: #008080; font-style: italic;">// KeyDown event of some control</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SomeControl_KeyDown<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, KeyEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// 1. Event is called directly after the key stroke</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// If the user hits Enter, we catch the</span>
    <span style="color: #008080; font-style: italic;">// event and do our own things</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">KeyCode</span> == Keys.<span style="color: #0000FF;">Enter</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Suppress key stroke, so that</span>
        <span style="color: #008080; font-style: italic;">// the control don't receives it</span>
        e.<span style="color: #0000FF;">SuppressKeyPress</span> = <span style="color: #0600FF;">true</span>;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Perform something important...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// KeyPress event of some control</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SomeControl_KeyPress<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, KeyPressEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// 2. Event is called during the key stroke</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// KeyUp event of some control</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SomeControl_KeyUp<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, KeyEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// 3. Event is called after the key stroke  </span>
<span style="color: #000000;">&#125;</span>

以上是关于csharp 在实际控制之前捕捉击键的主要内容,如果未能解决你的问题,请参考以下文章

显示所有组合键

Winform 中的键盘预览调用 Wpf 用户控件

击键清除psql中的屏幕?

MFC:击键后对话框不显示

如何等待特定的击键?

Fiddler如何捕捉DefaultHttpClient的HTTP请求