בסוף מצאתי את הקוד הבא, הוא מקפיץ פונקציה בעת לחיצת מקש רק בטרייד מסויים ולא בכל מקום במחשב:
Imports System
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
'קישור למקור הקוד
'http://social.msdn.microsoft.com/Forums/vstudio/en-US/84e8113e-f064-4695-8bf5-10ccdb756e8a/add-keyboard-shortcuts-to-custom-menu-items?forum=vsto
Public Class InterceptKeys
Private Const WH_KEYBOARD As Integer = 2 '13
Private Const HC_Action As Integer = 0
Private Shared proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
Private Shared hookID As IntPtr
Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer,
ByVal wParam As IntPtr,
ByVal lParam As IntPtr) As IntPtr
<DllImport("user32")> _
Private Shared Function SetWindowsHookEx(ByVal idHook As Integer,
ByVal lpfn As LowLevelKeyboardProcDelegate,
ByVal hMod As IntPtr,
ByVal dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function CallNextHookEx(ByVal hhk As IntPtr,
ByVal nCode As Integer,
ByVal wParam As IntPtr,
ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetKeyState(ByVal nVirtKey As Integer) As Short
End Function
Public Shared Sub HookShortCuts()
hookID = SetHook(proc)
End Sub
Public Shared Sub UnHookShortCuts()
UnhookWindowsHookEx(hookID)
End Sub
Private Shared Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
Return SetWindowsHookEx(WH_KEYBOARD, proc, IntPtr.Zero, CUInt(System.AppDomain.GetCurrentThreadId))
'Using curProcess As Process = Process.GetCurrentProcess
' Using curModule As ProcessModule = curProcess.MainModule
' Return SetWindowsHookEx(WH_KEYBOARD,
' proc,
' IntPtr.Zero,
' curProcess.Handle)
' End Using
'End Using
End Function
Const previousStateBit As Integer = 31 'Position of the previous state bit in lparam (1 if the key was down before the message is sent)
Const bitMask As Int32 = CType(2 ^ (previousStateBit - 1), Int32)
Private Shared Function HookCallback(ByVal nCode As Integer,
ByVal wParam As IntPtr,
ByVal lParam As IntPtr) As IntPtr
Dim keyWasAlreadyPressed As Boolean
If nCode < 0 Then
Return CallNextHookEx(hookID, nCode, wParam, lParam)
Else
If nCode = HC_Action Then
If wParam = Keys.D1 Then
keyWasAlreadyPressed = (CType(lParam, Int32) And bitMask) > 0
If (Not keyWasAlreadyPressed) And IsKeyDown(Keys.ControlKey) Then
MsgBox("Ctrl + 1")
Return CType(1, IntPtr)
End If
End If
End If
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End If
End Function
Private Shared Function IsKeyDown(ByVal k As Keys) As Boolean
Return (GetKeyState(k) And &HF0000000)
End Function
End Class
פורסם במקור בפורום CODE613 ב23/02/2014 23:15 (+02:00)