您好我在按下一组特定键时难以显示表单。主要形式先运行;这是第二种形式,它应该被隐藏,直到通过按ctrl + alt + shift + h切换。这是我的代码,我相信它应该有效,但事实并非如此。我试着这样做,在一个计时器中检查每1毫秒的新闻事件
Private Sub tmrKeys_Tick(sender As Object, e As EventArgs) Handles tmrKeys.Tick
Dim key As String = Nothing
If My.Computer.Keyboard.AltKeyDown AndAlso My.Computer.Keyboard.CtrlKeyDown AndAlso My.Computer.Keyboard.ShiftKeyDown AndAlso key = "H" Then
Dim x = New form2()
x.Show()
End If
End Sub
我还尝试了一个单独的功能:
Private Sub form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.Alt AndAlso e.Shift AndAlso e.KeyCode = Keys.H Then
form2.Show()
End If
End Sub
提前感谢您的帮助。
答案 0 :(得分:0)
这就是我要找的东西。感谢@jmcilhinney关于如何注册活动热键的RegisterHotKey提示。此代码将热键设置为alt + UPKEY。我想弄清楚如何修改参数以分配2个以上的键,如CTRL + ALT + SHIFT + UPKEY。但是我必须前往工作,所以今晚我将继续努力,明天为每个人更新,除非一个急切的海狸想要告诉我如何在我工作时这样做。再次感谢所有帮助过的人!
Public Class Form1
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Form2.Show()
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call UnregisterHotKey(Me.Handle, 9)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call RegisterHotKey(Me.Handle.ToInt32, 0, &H1, 38) '<-- registers specific hotkeys
End Sub
End Class
答案 1 :(得分:-1)
您的代码看起来不错(不需要计时器)。我认为您缺少的是KeyPreview
到True
当此属性设置为true时,表单将接收全部 KeyPress,KeyDown和KeyUp事件
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.KeyPreview = True
End Sub
然后这应该有效:
Private Sub form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.Alt AndAlso e.Shift AndAlso e.KeyCode = Keys.H Then
form2.Show()
End If
End Sub