玩酷之家

 找回密码
 立即注册
查看: 192|回复: 0

VB 禁止Win+D(显示桌面)快捷键

[复制链接]
发表于 2021-1-8 17:05:10 | 显示全部楼层 |阅读模式
本帖最后由 Ineverleft 于 2021-1-8 17:10 编辑

以上代码可以屏蔽系统热键,只能屏蔽ctrl+esc,win,win+d,win+e等,希望可以帮到你!
  1. '模块代码:
  2. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  3. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  4. Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
  5. Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
  6. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
  7. Private Const HC_ACTION = 0
  8. Private Const WM_KEYDOWN = &H100
  9. Private Const WM_KEYUP = &H101
  10. Private Const WM_SYSKEYDOWN = &H104
  11. Private Const WM_SYSKEYUP = &H105
  12. Private Const VK_TAB = &H9
  13. Private Const VK_CONTROL = &H11
  14. Private Const VK_ESCAPE = &H1B

  15. Public Const WH_KEYBOARD_LL = 13
  16. Private Const LLKHF_ALTDOWN = &H20

  17. Private Type KBDLLHOOKSTRUCT
  18.     vkCode As Long
  19.     scanCode As Long
  20.     flags As Long
  21.     time As Long
  22.     dwExtraInfo As Long
  23. End Type

  24. Dim p As KBDLLHOOKSTRUCT

  25. Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  26.    Dim fEatKeystroke As Boolean
  27.    
  28.    If (nCode = HC_ACTION) Then
  29.       If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
  30.          CopyMemory p, ByVal lParam, Len(p)
  31.          fEatKeystroke = _
  32.             ((p.vkCode = VK_TAB) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
  33.             ((p.vkCode = VK_ESCAPE) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
  34.             ((p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0)) Or _
  35.             ((p.vkCode = 91) Or (p.vkCode = 92) Or (p.vkCode = 93) Or (p.vkCode = 95) Or (p.vkCode = 255) Or (((p.flags And LLKHF_ALTDOWN) <> 0) And (p.vkCode = 115)))
  36.             End If
  37.     End If
  38.    
  39.     If fEatKeystroke Then
  40.         LowLevelKeyboardProc = 1
  41.     Else
  42.         LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
  43.     End If
  44. End Function


  45. '窗体代码
  46. Private hhkLowLevelKybd As Long
  47. Private Sub Form_Load()
  48. hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, Addressof LowLevelKeyboardProc, App.hInstance, 0)
  49. End Sub
  50. Private Sub Form_Unload(Cancel As Integer)
  51. If hhkLowLevelKybd Then UnhookWindowsHookEx hhkLowLevelKybd
  52. End Sub
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号 )

GMT+8, 2021-4-22 09:52 , Processed in 1.076402 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表