本文介绍了帮助实现全局键盘钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在VB2008中编写一个在系统托盘中运行的简单程序。我想设置一个热键组合(例如Ctrl + Alt + Shift + K),它将触发子程序。 到目前为止,我已经能够获取程序以响应组合键,但仅当表单未最小化到系统托盘时。以下是一些示例代码:I'm writing a simple program in VB2008 that runs in the system tray. I would like to set a hotkey combination (for instance Ctrl+Alt+Shift+K) that will trigger a subroutine.So far, I've been able to get the program to respond to a key combination, but only if the form is not minimized to the system tray. Here is some example code to demonstrate:Imports System.Runtime.InteropServicesPublic Partial Class MainFormPublic Const MOD_ALT As Integer = &H1Public Const MOD_CONTROL As Integer = &H2Public Const MOD_SHIFT As Integer = &H4Public Const WM_HOTKEY As Integer = &H312<DllImport("User32.dll")> _Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _ ByVal id As Integer, ByVal fsModifiers As Integer, _ ByVal vk As Integer) As IntegerEnd Function<DllImport("User32.dll")> _Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _ ByVal id As Integer) As IntegerEnd FunctionPublic Sub New()Me.InitializeComponent()End SubSub MainFormLoad(sender As Object, e As EventArgs)RegisterHotKey(Me.Handle, 100, MOD_CONTROL + MOD_ALT + MOD_SHIFT, Keys.K)ContextMenuStrip1.Enabled = FalseEnd SubProtected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)If m.Msg = WM_HOTKEY ThenDim id As IntPtr = m.WParamSelect Case (id.ToString)Case "100"MsgBox("Ctrl + Alt + Shift + K was pressed")End SelectEnd IfMyBase.WndProc(m)End SubSub MainFormFormClosing(sender As Object, e As FormClosingEventArgs)e.Cancel = TrueMe.WindowState = FormWindowState.MinimizedMe.ShowInTaskbar = FalseContextMenuStrip1.Enabled = TrueEnd SubSub ShowToolStripMenuItemClick(sender As Object, e As EventArgs)Me.WindowState = FormWindowState.NormalMe.ShowInTaskbar = TrueContextMenuStrip1.Enabled = FalseEnd SubSub ExitToolStripMenuItemClick(sender As Object, e As EventArgs)UnregisterHotKey(Me.Handle, 100)EndEnd SubSub MainFormSizeChanged(sender As Object, e As EventArgs)If Me.WindowState = FormWindowState.Minimized ThenShowInTaskbar = FalseContextMenuStrip1.Enabled = TrueEnd IfEnd SubEnd Class 如果我将 ShowInTaskbar 更改为 True 在MainFormSizeChanged处理程序中,程序将响应键组合,即使最小化到系统托盘(可能是因为它仍在任务栏中)。虽然这是一个解决方案,但它并不像它应该的那样正确。 我认为解决方案是使用全局键盘钩,但这超出了我的经验水平。 我已经为此代码 [ ^ ](使用键盘钩子捕获printscreen键)并确认键盘钩子即使最小化到托盘,代码也能正常工作。但是我很难看到如何调整代码以使用我的组合键。 一个稍微不同的keyhook实现可以在这段代码 [ ^ ]。我还为此代码添加了系统托盘功能,它在最小化时也能正常工作。但同样,我无法实现这一目标,并对其进行调整以响应我的组合键。 是否有人有任何代码建议或链接可能对我有帮助得到这个吗?If I change ShowInTaskbar to True in the MainFormSizeChanged handler, the program will respond to the key combination even when minimized to the system tray (presumably because it is still in the taskbar). While this is a solution, its not as 'proper' as it should be.I think the solution is to use a global keyboard hook, but this is going beyond my level of experience.I've added a notify icon and context menu script to this code[^](which uses a keyboard hook to capture the printscreen key) and confirmed that the keyboard hook code does work even when minimized to the tray. But I'm having a hard time seeing how to adapt the code to work with my key combination.A slightly different keyhook implementation can be found in this code[^]. I also added systray functionality to this code and it too works when minimized. But again, I'm not able to make the leap and adapt this to respond to my key combination.Does anyone have any code suggestions or links that might help me get this working?推荐答案 您没有显示 MainFormLoad 叫做。有可能在那时表单句柄还没有准备好。稍后再做,例如,在 Form.Shown 的处理程序中。 更真实的问题:当窗口最小化时,你希望显示消息框,并说问题不是消息框。但实际上这是一个问题。表单被隐藏/最小化,因此激活了一些其他应用程序。实际显示消息框,但隐藏在顶部激活的窗口后面。仔细检查。You did not show where MainFormLoad is called. It is possible that at that moment the form handle is not ready yet. Do it later, for example, in the handler of Form.Shown.More real problem: when a window is minimized, you expected to show the message box, and say that the problem is not the message box. But it is actually a problem. The form is hidden/minimized, so some other application is activated. The message box is actually shown, but hidden behind the activated window on top. Check up carefully. -SA 这篇关于帮助实现全局键盘钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 06:17