如何對 Ctrl+Alt+Del、Alt+Tab、Ctrl+Esc、Alt+F4等組合Hot Key 做鍵盤攔截
關於 鍵盤攔截 ( Ctrl+Alt+Del、Alt+Tab、Ctrl+Esc、Alt+F4、M$ 等組合Hot Key )
VB.Net 寫法 :
' API 宣告
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ControlTask (True)
End Sub
Private Sub Form1_Closed _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
ControlTask (False)
End Sub
Private Sub ControlTask(ByVal blnFlag As Boolean)
Dim strKey As String
blnFlag = Not blnFlag
KeyPreview = True
strKey = "HKCU\software\microsoft\windows\currentversion\policies\"
With CreateObject("WScript.Shell")
.RegWrite(strKey & "system\DisableTaskMgr", blnFlag)
.RegWrite(strKey & "Explorer\NoLogoff", blnFlag)
.RegWrite(strKey & "Explorer\NoClose", blnFlag)
.RegWrite(strKey & "system\DisableLockWorkstation", blnFlag)
.RegWrite(strKey & "system\DisableChangePassword", blnFlag)
.RegWrite(strKey & "Explorer\NoViewContextMenu", blnFlag)
.RegWrite(strKey & "Explorer\NoChangeStartMenu", blnFlag)
.RegWrite(strKey & "Explorer\NoRun", blnFlag)
.RegWrite(strKey & "Explorer\NoSetTaskbar", blnFlag)
End With
End Sub
Private Sub Form1_KeyDown _
(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim KeyCode As Short = e.KeyCode
Dim Shift As Short = e.KeyData \ 65536
If KeyCode = 91 Or KeyCode = 92 Then
keybd_event(20, 0, 2, 0) ' M$
keybd_event(91, 0, 2, 0) ' M$
End If
If Shift = 4 And KeyCode = 18 Then keybd_event(18, 0, 2, 0) 'Alt+Tab
If Shift = 2 And KeyCode = 17 Then keybd_event(17, 0, 2, 0) 'Ctrl+Esc
If Shift = 4 And KeyCode = 115 Then keybd_event(115, 0, 2, 0) 'Alt+F4
If KeyCode = 112 Then keybd_event(112, 0, 2, 0) 'F1
KeyCode = 0
End Sub
================================================================
VB6 寫法 :
' API 宣告
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Form_Load()
ControlTask True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ControlTask False
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 91 Or KeyCode = 92 Then
keybd_event 20, 0, 2, 0 ' M$
keybd_event 91, 0, 2, 0 ' M$
End If
If Shift = 4 And KeyCode = 18 Then keybd_event 18, 0, 2, 0 'Alt+Tab
If Shift = 2 And KeyCode = 17 Then keybd_event 17, 0, 2, 0 'Ctrl+Esc
If Shift = 4 And KeyCode = 115 Then keybd_event 115, 0, 2, 0 'Alt+F4
If KeyCode = 112 Then keybd_event 112, 0, 2, 0 'F1
KeyCode = 0
End Sub
Public Sub ControlTask(ByVal Show As Boolean)
Dim strKey As String
Show = Not Show
strKey = "HKCU\software\microsoft\windows\currentversion\policies\"
With CreateObject("WScript.Shell")
.RegWrite strKey & "system\DisableTaskMgr", Show
.RegWrite strKey & "Explorer\NoLogoff", Show
.RegWrite strKey & "Explorer\NoClose", Show
.RegWrite strKey & "system\DisableLockWorkstation", Show
.RegWrite strKey & "system\DisableChangePassword", Show
.RegWrite strKey & "Explorer\NoViewContextMenu", Show
.RegWrite strKey & "Explorer\NoChangeStartMenu", Show
.RegWrite strKey & "Explorer\NoRun", Show
.RegWrite strKey & "Explorer\NoSetTaskbar", Show
End With
End Sub