'Event typesPrivate Const WM_KEYUP As Integer = &H101Private Const WM_KEYDOWN As Short = &H100SPrivate Const WM_SYSKEYDOWN As Integer = &H104Private Const WM_SYSKEYUP As Integer = &H105'Event Info structurePublic Structure KBDLLHOOKSTRUCTPublic vkCode As IntegerPublic scanCode As IntegerPublic flags As IntegerPublic time As IntegerPublic dwExtraInfo As IntegerEnd Structure'Keyboard hook related functionsPrivate Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As IntegerPrivate Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As IntegerPrivate Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As IntegerPrivate Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As IntegerPrivate Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntegerPrivate KeyboardHandle As IntPtr = 0 'Handle of the hookPrivate callback As KeyboardHookDelegate = Nothing 'Delegate for the hookPrivate Function Hooked()Return KeyboardHandle <> 0 'If KeyboardHandle = 0 it means that it isn't hooked so return false, otherwise return trueEnd FunctionPublic Sub HookKeyboard()callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)If KeyboardHandle <> 0 ThenDebug.Write(vbCrLf & "[Keyboard Hooked]" & vbCrLf)End IfEnd SubPublic Sub UnhookKeyboard()If (Hooked()) ThenIf UnhookWindowsHookEx(KeyboardHandle) <> 0 ThenDebug.Write(vbCrLf & "[Keyboard Unhooked]")KeyboardHandle = 0 'We have unhooked successfullyEnd IfEnd IfEnd SubPublic Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer'Variable to hold the text describing the key pressedDim Key = lParam.vkCode'If event is KEYDOWNIf wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then'If we can block eventsIf Code >= 0 ThenIf Key = 91 Or Key = 92 ThenReturn 1 'Block eventEnd IfEnd IfEnd IfDebug.Write(Key)'Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applicationsEnd FunctionPrivate Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosingUnhookKeyboard()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadHookKeyboard()End Sub
Sumber: https://www.daniweb.com/programming/software-development/threads/420235/disabling-windows-key-in-vb-net

This comment has been removed by the author.
ReplyDelete