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;">(</span><span style="color: #FF0000;">object</span> sender, KeyEventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// 1. Event is called directly after the key stroke</span>
<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;">(</span>e.<span style="color: #0000FF;">KeyCode</span> == Keys.<span style="color: #0000FF;">Enter</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</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>;
<span style="color: #008080; font-style: italic;">// Perform something important...</span>
<span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>
<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;">(</span><span style="color: #FF0000;">object</span> sender, KeyPressEventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// 2. Event is called during the key stroke</span>
<span style="color: #000000;">}</span>
<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;">(</span><span style="color: #FF0000;">object</span> sender, KeyEventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// 3. Event is called after the key stroke </span>
<span style="color: #000000;">}</span>
以上是关于csharp 在实际控制之前捕捉击键的主要内容,如果未能解决你的问题,请参考以下文章