当前位置:编程学习 > VB >>

VB如何设置禁止通过热键启动任何软件?

就是启动软件之后,任何热键都禁用了。

追问:方便的话,帮我看看能不能这样,禁止Win键的同时,也禁止Alt+Ctrl+delete键?
答案:VB禁止win键(修改可以禁止任意热键)

'把以下程序粘贴到模块中

'***************************

Option Explicit
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private 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
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT

    vkCode As Long

    scanCode As Long

    flags As Long

    time As Long

    dwExtraInfo As Long

End Type


Private Const WM_KEYDOWN = &H100

Private Const WM_SYSKEYDOWN = &H104

Private Const WM_KEYUP = &H101

Private Const WM_SYSKEYUP = &H105


Private Const VK_LWIN = &H5B

Private Const VK_RWIN = &H5C


Private Const HC_ACTION = 0

Private Const WH_KEYBOARD_LL = 13


Private lngHook As Long


'使用底层KeyboardHook拦截按键消息

Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Dim blnHook As Boolean

    Dim p As PKBDLLHOOKSTRUCT

   

    If nCode = HC_ACTION Then

        Select Case wParam

            Case WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP

                Call CopyMemory(p, ByVal lParam, Len(p))

                If p.vkCode = VK_LWIN Or p.vkCode = VK_RWIN Then '按下了左/右Win键

                    blnHook = True

                End If

            Case Else

                'do nothing

        End Select

    End If

   

    If blnHook Then

        LowLevelKeyboardProc = 1

    Else

        Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)

    End If

End Function


Public Sub HooK()

    lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)

End Sub


Public Sub UnHooK()

    Call UnhookWindowsHookEx(lngHook)

End Sub

'***************************

'把以下程序粘贴到窗体中

'***************************

Private Sub Form_Load()

    Hook

End Sub

Private Sub Form_Unload(Cancel As Integer)

    UnHook

End Sub

键盘常用ASCII码 2007-04-13 16:31

ESC键   VK_ESCAPE (27)
回车键: VK_RETURN (13)
TAB键:   VK_TAB (9)
Caps Lock键: VK_CAPITAL (20)
Shift键: VK_SHIFT ($10)
Ctrl键: VK_CONTROL (17)
Alt键:   VK_MENU (18)
空格键: VK_SPACE ($20/32)
退格键: VK_BACK (8)
左徽标键: VK_LWIN (91)
右徽标键: VK_LWIN (92)
鼠标右键快捷键:VK_APPS (93)

Insert键: VK_INSERT (45)
Home键: VK_HOME (36)
Page Up: VK_PRIOR (33)
PageDown: VK_NEXT (34)
End键:   VK_END (35)
Delete键: VK_DELETE (46)

方向键(←): VK_LEFT (37)
方向键(↑): VK_UP (38)
方向键(→): VK_RIGHT (39)
方向键(↓): VK_DOWN (40)


F1键:   VK_F1 (112)
F2键:   VK_F2 (113)
F3键:   VK_F3 (114)
F4键:   VK_F4 (115)
F5键:   VK_F5 (116)
F6键:   VK_F6 (117)
F7键:   VK_F7 (118)
F8键:   VK_F8 (119)
F9键:   VK_F9 (120)
F10键:   VK_F10 (121)
F11键:   VK_F11 (122)
F12键:   VK_F12 (123)


Num Lock键: VK_NUMLOCK (144)
小键盘0: VK_NUMPAD0 (96)
小键盘1: VK_NUMPAD0 (97)
小键盘2: VK_NUMPAD0 (98)
小键盘3: VK_NUMPAD0 (99)
小键盘4: VK_NUMPAD0 (100)
小键盘5: VK_NUMPAD0 (101)
小键盘6: VK_NUMPAD0 (102)
小键盘7: VK_NUMPAD0 (103)
小键盘8: VK_NUMPAD0 (104)
小键盘9: VK_NUMPAD0 (105)
小键盘.: VK_DECIMAL (110)
小键盘*: VK_MULTIPLY (106)
小键盘+: VK_MULTIPLY (107)
小键盘-: VK_SUBTRACT (109)
小键盘/: VK_DIVIDE (111)


Pause Break键: VK_PAUSE (19)
Scroll Lock键: VK_SCROLL (145)

这么做,不合适吧。

当然也能,可以做个屏蔽列表。

但是。。。太易做图了吧。

 

而且工作量蛮大,不同操作系统,也不一样,要分别写代码的。

如果是学习的角度,建议你参考MSDN,

如果是干别的,你可以用第三方控件实现,我记得有人放出来过类似的模块。

上一个:请问如何用VB程序来模拟键盘操作
下一个:关于VB的块IF嵌套问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,