Tác giả: vo_minhdat2007
Mô tả: Các phím như Alt+Tab, Alt+F4, ... tuy nhiên chỉ áp dụng khi chạy dạng Release (Built rồi chạy file exe), không có tác dụng khi debug! Ngoài ra cũng áp dụng được với VB2005 (chưa test)
---------------------------------------------------
Đầu tiên, cho đoạn code sau vào 1 Module :
- Imports System.Runtime.InteropServices
- Imports System.Reflection
- Module Hook 'ở đây tên module là hook, bạn đổi thành tên module bạn đã đặt
- Region "Declarations"
- Private Const HC_ACTION As Integer = 0
- Private Const WH_KEYBOARD_LL As Integer = 13&
- Private Structure KBDLLHookStruct
- Public vkCode As Integer
- Public scanCode As Integer
- Public flags As Integer
- Public time As Integer
- Public dwExtraInfo As Integer
- End Structure
- <MarshalAs(UnmanagedType.FunctionPtr)> _
- Private callback As KeyboardHookDelegate
- Private mbBlockKeys As Boolean = True
- Private miKeyboardHandle As Integer = 0
- Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, _
- ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
- Private Declare Function CallNextHookEx Lib "user32" ( _
- ByVal hHook As Integer, ByVal nCode As Integer, _
- ByVal wParam As Integer, ByVal lParam As KBDLLHookStruct) As Integer
- Private Declare Function SetWindowsHookEx Lib "user32" _
- Alias "SetWindowsHookExA" _
- (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, _
- ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
- Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
- ByVal hHook As Integer) As Integer
- End Region
- Region "Properties"
- Public Property BlockKeyCombinations() As Boolean
- Get
- Return mbBlockKeys
- End Get
- Set(ByVal value As Boolean)
- mbBlockKeys = value
- End Set
- End Property
- Public ReadOnly Property IsHooked() As Boolean
- Get
- Return miKeyboardHandle <> 0
- End Get
- End Property
- End Region
- Public Function IsInIDE() As Boolean
- Return System.Diagnostics.Debugger.IsAttached
- End Function
- Public Sub HookKeyboard()
- ' Release any existing keyboard hook.
- UnhookKeyboard()
- If Not IsInIDE() Then
- callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
- miKeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, _
- Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
- End If
- End Sub
- Public Sub UnhookKeyboard()
- If (IsHooked()) Then Call UnhookWindowsHookEx(miKeyboardHandle)
- End Sub
- Private Function BlockKeyCombination(ByVal Hookstruct As KBDLLHookStruct) As Boolean
- Dim bResult As Boolean = False
- If mbBlockKeys Then
- Select Case Hookstruct.vkCode
- Case System.ConsoleKey.Escape
- If My.Computer.Keyboard.CtrlKeyDown Then
- Debug.Print("Blocking: Ctrl-Esc")
- bResult = True
- ElseIf My.Computer.Keyboard.AltKeyDown Then
- Debug.Print("Blocking: Alt-Esc")
- bResult = True
- End If
- Case System.ConsoleKey.Tab
- If My.Computer.Keyboard.AltKeyDown Then
- Debug.Print("Blocking: Alt-Tab")
- bResult = True
- End If
- Case System.ConsoleKey.RightWindows, System.ConsoleKey.LeftWindows
- Debug.Print("Blocking: Windows Key")
- bResult = True
- Case System.ConsoleKey.Applications
- Debug.Print("Blocking: Application Key")
- bResult = True
- Case System.ConsoleKey.F4
- If My.Computer.Keyboard.AltKeyDown Then
- Debug.Print("Blocking: Alt-F4")
- bResult = True
- End If
- Case Else
- End Select
- End If
- Return bResult
- End Function
- Private Function KeyboardCallback(ByVal Code As Integer, _
- ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
- Dim lResult As Integer = 0
- If (Code = HC_ACTION) AndAlso (BlockKeyCombination(lParam)) Then
- lResult = 1
- Else
- lResult = CallNextHookEx(miKeyboardHandle, Code, wParam, lParam)
- End If
- Return lResult
- End Function
- End Module
Xong rồi, giờ trong phần Application Events, trong sự kiện Startup, thêm đoạn code :
- HookKeyboard()
- BlockKeyCombinations = True
Bất cứ lúc nào muốn ngừng hoặc bật thì hãy cho BlockKeyCombinations bằng False/True
Và trong sự kiện Shutdown, đừng quên trả lại nhé!