HtmlElement.KeyDown 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当用户按下键盘上的某个键时发生。
public:
event System::Windows::Forms::HtmlElementEventHandler ^ KeyDown;
public event System.Windows.Forms.HtmlElementEventHandler KeyDown;
public event System.Windows.Forms.HtmlElementEventHandler? KeyDown;
member this.KeyDown : System.Windows.Forms.HtmlElementEventHandler
Public Custom Event KeyDown As HtmlElementEventHandler
事件类型
示例
下面的代码示例演示了此成员的用法。 在此示例中,事件处理程序报告事件的发生情况 KeyDown 。 此报表可帮助你了解事件发生的时间,并可以帮助你进行调试。 若要报告多个事件或频繁发生的事件,请考虑将 MessageBox.Show 替换为 Console.WriteLine 或将消息追加到多行 TextBox。
若要运行示例代码,请将其粘贴到包含名为 HtmlElement1
的 类型的HtmlElement实例的项目中。 然后,确保事件处理程序与 KeyDown 事件相关联。
private void HtmlElement1_KeyDown(Object sender, HtmlElementEventArgs e) {
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "MouseButtonsPressed", e.MouseButtonsPressed );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ClientMousePosition", e.ClientMousePosition );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "OffsetMousePosition", e.OffsetMousePosition );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "MousePosition", e.MousePosition );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "BubbleEvent", e.BubbleEvent );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "KeyPressedCode", e.KeyPressedCode );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "AltKeyPressed", e.AltKeyPressed );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "CtrlKeyPressed", e.CtrlKeyPressed );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ShiftKeyPressed", e.ShiftKeyPressed );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "EventType", e.EventType );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ReturnValue", e.ReturnValue );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "FromElement", e.FromElement );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ToElement", e.ToElement );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "KeyDown Event" );
}
Private Sub HtmlElement1_KeyDown(sender as Object, e as HtmlElementEventArgs) _
Handles HtmlElement1.KeyDown
Dim messageBoxVB as New System.Text.StringBuilder()
messageBoxVB.AppendFormat("{0} = {1}", "MouseButtonsPressed", e.MouseButtonsPressed)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "ClientMousePosition", e.ClientMousePosition)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "OffsetMousePosition", e.OffsetMousePosition)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "MousePosition", e.MousePosition)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "BubbleEvent", e.BubbleEvent)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "KeyPressedCode", e.KeyPressedCode)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "AltKeyPressed", e.AltKeyPressed)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "CtrlKeyPressed", e.CtrlKeyPressed)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "ShiftKeyPressed", e.ShiftKeyPressed)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "EventType", e.EventType)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "ReturnValue", e.ReturnValue)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "FromElement", e.FromElement)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "ToElement", e.ToElement)
messageBoxVB.AppendLine()
MessageBox.Show(messageBoxVB.ToString(),"KeyDown Event")
End Sub
注解
KeyDown 发生在 之前 KeyPress,后者又发生在 之前 KeyUp。
通过将 类true
的 HtmlElementEventArgs 属性设置为 ReturnValue ,可以取消元素上事件的默认操作KeyDown。
KeyDown元素上的事件也会在该元素的父元素和类本身上HtmlDocument发生,除非将 类的 HtmlElementEventArgs 属性设置为 BubbleEventtrue
。