|
-
Jan 24th, 2008, 08:56 PM
#1
Thread Starter
Hyperactive Member
[Resolved] Disable ALT-F4 (Globally)
Hello All,
I am writing some kiosk software, and I am looking for a way to disable the ATL+F4 key combination, globally. I have seen some clever code on this forum, but nothing that would fill my needs, precisely. It is a simple matter to make my program ignore ATL-F4, but I need to block this key combination for all applications, as I don't want someone to close programs, and ultimately, Windows in this way.
Could someone show me how to do this with APIs, or point me in the right direction?
Many Thanks!
Last edited by Quiver318; Jan 25th, 2008 at 10:19 PM.
-
Jan 24th, 2008, 11:05 PM
#2
Re: Disable ALT-F4 (Globally)
well, here's some code for vb6. You didn't specify your vb but here you go. It's disables alt-esc and then windows key. You can see how it works though and use it to block ANY key. Basically you reroute all keypresses through the sub in the module and prevent ones you don't like from getting back to the OS.
Code:
Private Const WH_KEYBOARD_LL = 13& 'enables monitoring of keyboard
'input events about to be posted
'in a thread input queue
Private Const HC_ACTION = 0& 'wParam and lParam parameters
'contain information about a
'keyboard message
Private Const LLKHF_EXTENDED = &H1& 'test the extended-key flag
Private Const LLKHF_INJECTED = &H10& 'test the event-injected flag
Private Const LLKHF_ALTDOWN = &H20& 'test the context code
Private Const LLKHF_UP = &H80& 'test the transition-state flag
Private Const VK_TAB = &H9 'virtual key constants
Private Const VK_CONTROL = &H11
Private Const VK_ESCAPE = &H1B
Private Type KBDLLHOOKSTRUCT
vkCode As Long 'a virtual-key code in the range 1 to 254
scanCode As Long 'hardware scan code for the key
flags As Long 'specifies the extended-key flag,
'event-injected flag, context code,
'and transition-state flag
time As Long 'time stamp for this message
dwExtraInfo As Long 'extra info associated with the message
End Type
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 Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal cb As Long)
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private m_hDllKbdHook As Long 'private variable holding
'the handle to the hook procedure
Public Sub Main()
'set and obtain the handle to the keyboard hook
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0&)
If m_hDllKbdHook <> 0 Then
'It's hooked! Show a messagebox
'to temporarily suspend the app here
'(the LowLevelKeyboardProc will continue
'to process messages), and follow the
'messagebox, for this demo, with the
'unhook call. See the text above for
'specific information.
MsgBox "Ctrl+Esc, Alt+Tab and Alt+Esc are blocked. " & _
"Click OK to quit and re-enable the keys.", _
vbOKOnly Or vbInformation, _
"Keyboard Hook Active"
'Placement for this demo only.
'Move to the unload event of the
'main form when used in an application.
Call UnhookWindowsHookEx(m_hDllKbdHook)
Else
MsgBox "Failed to install low-level keyboard hook - " & Err.LastDllError
End If
End Sub
Public Function LowLevelKeyboardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Application-defined callback function
'used with the SetWindowsHookEx function.
'The system calls this function every
'time a new keyboard input event is about
'to be posted into a thread input queue.
'The keyboard input can come from the local
'keyboard driver or from calls to the
'keybd_event function. If the input comes
'from a call to keybd_event, the input
'was "injected".
Static kbdllhs As KBDLLHOOKSTRUCT
'If nCode is less than zero, the hook
'procedure must return the value returned
'by CallNextHookEx.
'
'If nCode is greater than or equal to zero,
'and the hook procedure did not process the
'message, it is highly recommended that you
'call CallNextHookEx and return the value it
'returns; otherwise, other applications that
'have installed WH_KEYBOARD_LL hooks will not
'receive hook notifications and may behave
'incorrectly as a result.
'
'If the hook procedure processed the message,
'it may return a nonzero value to prevent the
'system from passing the message to the rest
'of the hook chain or the target window procedure.
If nCode = HC_ACTION Then
'nCode specifies a code the hook
'procedure uses to determine how
'to process the message. HC_ACTION
'is the only valid code.
'On receipt of the HC_ACTION code,
'wParam and lParam contain information
'about a keyboard message, and lParam
'holds the pointer to a KBDLLHOOKSTRUCT
'structure.
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
'Ctrl+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And _
CBool(GetAsyncKeyState(VK_CONTROL) _
And &H8000) Then
Debug.Print "Ctrl+Esc blocked"
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_ESCAPE
'Alt+Tab --------------
If (kbdllhs.vkCode = VK_TAB) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then
Debug.Print "Alt+Tab blocked"
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_TAB
'Alt+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then
Debug.Print "Alt+Esc blocked"
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_ESCAPE
End If 'nCode = HC_ACTION
LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, _
nCode, _
wParam, _
lParam)
End Function
-
Jan 25th, 2008, 07:35 AM
#3
Thread Starter
Hyperactive Member
Re: Disable ALT-F4 (Globally)
Greetings, Lord Orwell. This is a terrific start, thank you!
Sadly, I get an error when running the code on Windows XP Home, and on VB6 (SP6). The only change I made was the name of the Sub Main. I changed Public Sub Main() to Public Sub Form_Load(), and ran the project, but it threw an error. The error is: Compile Error - Invalid use of the AddressOf Operator
The program haults on this line of the program, which is the first line in the Public Sub Form_Load() sub:
Code:
'set and obtain the handle to the keyboard hook
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0&)
If you could, please advise me on the correct use of the AddressOf operator. Thank you!
-
Jan 25th, 2008, 08:17 AM
#4
Member
Re: Disable ALT-F4 (Globally)
Greetings to both of you.
You must put the code done by Lord Orwell to the MODULE the set the startup object to Sub Main then call the Form you want to Appear. Code like these
Public Sub Main()
'set and obtain the handle to the keyboard hook
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0&)
If m_hDllKbdHook <> 0 Then
'It's hooked! Show a messagebox
'to temporarily suspend the app here
'(the LowLevelKeyboardProc will continue
'to process messages), and follow the
'messagebox, for this demo, with the
'unhook call. See the text above for
'specific information.
MsgBox "Ctrl+Esc, Alt+Tab and Alt+Esc are blocked. " & _
"Click OK to quit and re-enable the keys.", _
vbOKOnly Or vbInformation, _
"Keyboard Hook Active"
'Placement for this demo only.
'Move to the unload event of the
'main form when used in an application.
'Call UnhookWindowsHookEx(m_hDllKbdHook)
Form1.Show
Else
MsgBox "Failed to install low-level keyboard hook - " & Err.LastDllError
End If
End Sub
To Lord Orwell, thanks for these code. This really put me out of the wilderness
-
Jan 25th, 2008, 09:17 AM
#5
Re: Disable ALT-F4 (Globally)
all that code must be in the same module or form.
-
Jan 25th, 2008, 09:40 AM
#6
Thread Starter
Hyperactive Member
Re: Disable ALT-F4 (Globally)
Thanks Jorgo, and Lord Orwell.
I was able to get the code running after adding it to a module, and it works great for blocking the originally assigned key combinations. I do have a question about the ALT+F4 trapping, however.
I added the virtual key constant for the F4 key, as seen in the code segment below, but the project snaps shut instead of trapping or blocking ALT+F4 as it does with the original key assignments. Do you have any tips for making ALT+F4 work as well?
Code:
If (kbdllhs.vkCode = VK_F4) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then
Debug.Print "Alt+F4 blocked"
LowLevelKeyboardProc = 1
Exit Function
End If
Last edited by Quiver318; Jan 25th, 2008 at 10:53 AM.
-
Jan 25th, 2008, 12:01 PM
#7
Member
Re: Disable ALT-F4 (Globally)
We have same code below but mine is Working.
Code:
If (kbdllhs.vkCode = VK_F4) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then
Debug.Print "Alt+F4 blocked"
LowLevelKeyboardProc = 1
Exit Function
End If
[/QUOTE]
This was also happen to me when i copy and paste the code. But when i open the Module then it's working.
-
Jan 25th, 2008, 07:00 PM
#8
Thread Starter
Hyperactive Member
Re: Disable ALT-F4 (Globally)
Thanks for the report, Jorgo. But, I am using the same code from a .bas module, and ATL-F4 is not blocked for me. All other original keys are blocked, however.
May I ask if you using the same constant as the one below?
Code:
Private Const VK_F4 = &H73
-
Jan 25th, 2008, 10:18 PM
#9
Thread Starter
Hyperactive Member
Re: Disable ALT-F4 (Globally)
I figured it out!
It turns out that during a crash, my startup object reverted to being Form1, so Sub Main () was not even loading. The code works great!
Thanks, Lord Orwell. You saved me some real time with this clever solution.
I hope it works on Vista as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|