在VB.NET中捕获功能键F1..F12 [英] Capture function keys F1..F12 in VB.NET

查看:287
本文介绍了在VB.NET中捕获功能键F1..F12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为我的应用程序捕获功能键 F1 .. F12 .我能够捕获常规键和修饰符,例如 shift ctrl alt 等.

I cannot capture the function keys F1..F12 for my application. I am able to capture regular keys and modifiers such as shift, ctrl, alt, etc..

问题建议KeyPreview = True,但是对于我的应用程序似乎不起作用.我在做什么错了?

This question recommends KeyPreview = True, however that does not seem to work for my application. What am I doing wrong?

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.KeyPreview = True
    AddHandler Me.KeyDown, AddressOf KeyDownsHandler
    AddHandler Me.KeyUp, AddressOf KeyUpHandler
End Sub

Private Sub KeyUpHandler(ByVal o As Object, ByVal e As KeyEventArgs)
    e.SuppressKeyPress = True
    If e.KeyCode = Keys.F1 Then
        txtMain.AppendText("F1 was pressed!" & Environment.NewLine)
    End If
    txtMain.AppendText( _
        String.Format("'{0}' '{1}' '{2}' '{3}' {4}", e.Modifiers, e.KeyValue, e.KeyData, e.KeyCode, Environment.NewLine))
End Sub

Private Sub KeyDownHandler(ByVal o As Object, ByVal e As KeyEventArgs)
    e.SuppressKeyPress = True
    txtMain.AppendText( _
        String.Format("'{0}' '{1}' '{2}' '{3}' {4}", e.Modifiers, e.KeyValue, e.KeyData, e.KeyCode, Environment.NewLine))
End Sub

推荐答案

您的代码对我有用,但EventHandler声明中有错字.更改:

Your code works for me with the exception that you have a typo in your EventHandler declaration. Change:

AddHandler Me.KeyDown, AddressOf KeyDownsHandler

AddHandler Me.KeyDown, AddressOf KeyDownHandler

这篇关于在VB.NET中捕获功能键F1..F12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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