PDA

View Full Version : How to disable ALT+TAB,CTRL+ESC, and Windows key using VBA



Yance
08-30-2009, 06:56 PM
Hi all..

I'm trying to make an internet cafe billing application using Microsoft Access. Any one can help me how to disable ALT+TAB, CTRL+ESC, and Windows button function using access VBA? I can do that in VB using SetWindowsHookEx API function. But in access vba it's not running well because the variable/object/property "App" does not exist in Microsoft Access. There is the way to do that in Microsoft Access VBA?

Thanks

geekgirlau
09-03-2009, 12:26 AM
I don't know the answer to your question, but can I ask why you would want to do this? As a user I would be extremely unhappy if any application disabled these keystrokes as I use them constantly.

Yance
09-03-2009, 08:08 AM
I don't know the answer to your question, but can I ask why you would want to do this? As a user I would be extremely unhappy if any application disabled these keystrokes as I use them constantly.

Thanks for your response. But i think you don't read my post well. I've said that i try to make INTERNET CAFE BILLING application. Therefore i need to disable the function of ALT+TAB, CTRL+ESC, & WINDOWS key to prevent users to accessing computer before they login to billing application. I can do that in my vb project, but i don't know how to make my code in vb can run in access vba too.

Oorang
09-03-2009, 11:47 AM
GG has a good point, but for an I-Cafe I could see why you might want to do that.
I haven't tried to do this myself, but one approach to consider would be to make your database using Access's "Data Access Pages" and for your application's "front end" and then just using your application via internet explorer in "kiosk mode (http://support.microsoft.com/kb/154780)".

This won't completely lock down the desktop, but it get's you a hair closer. If you have a copy of the original VB6 code, I might be able to help you adapt it.

Yance
09-03-2009, 06:17 PM
Thank you Oorang..

Here my VB6 code:

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_STARTKEY = &H5B


Public Const WH_KEYBOARD_LL = 13
Public Const LLKHF_ALTDOWN = &H20

Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type

Public hhkLowLevelKybd
Dim p As KBDLLHOOKSTRUCT

Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean

If (nCode = HC_ACTION) Then
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
CopyMemory p, ByVal lParam, Len(p)
fEatKeystroke = _
((p.vkCode = VK_TAB) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0)) Or _
p.vkCode = VK_STARTKEY

End If
End If

If fEatKeystroke Then
LowLevelKeyboardProc = -1
Else
LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End If
End Function

To run this function, i add this code below in Form_Load Event:

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

The problem is on "App.hInstance". I don't know the Access VBA object/property for that.

Yance
09-03-2009, 06:33 PM
Thank you Oorang..

I don't have any problem with database. But your input gives me a new inspiration.

Here my VB6 code:

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_STARTKEY = &H5B


Public Const WH_KEYBOARD_LL = 13
Public Const LLKHF_ALTDOWN = &H20

Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type

Public hhkLowLevelKybd
Dim p As KBDLLHOOKSTRUCT

Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean

If (nCode = HC_ACTION) Then
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
CopyMemory p, ByVal lParam, Len(p)
fEatKeystroke = _
((p.vkCode = VK_TAB) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0)) Or _
p.vkCode = VK_STARTKEY

End If
End If

If fEatKeystroke Then
LowLevelKeyboardProc = -1
Else
LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End If
End Function

To run this function, i add this code below in Form_Load Event:

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

The problem is on "App.hInstance". I don't know the Access VBA object/property for that.

Yance
09-03-2009, 06:41 PM
Sorry for my double post, there is problem with my internet connection.

Oorang
09-03-2009, 08:28 PM
Haven't tested this, but to get the form's handle just use me.Hwnd (from inside the form). If you want the handle to Access itself you would use Access.hWndAccessApp. Let me know if that works?

Yance
09-03-2009, 09:15 PM
No, i've tried it before and it doesn't work. That is mean we can't do that in access vba?

Yance
09-03-2009, 09:31 PM
Currently i just write a code in Form_Timer event to make access window always on top and disable the task manager. So, although users press the ALT+TAB, CTRL+ESC, WINDOWS key on the keyboard, they never can't use or got focus to another application before successfull login to the my i-cafe application (just like using internet explorer in "kiosk mode").

Oorang
09-05-2009, 07:04 PM
Nice solution:) I'd like to know how it tests out. Would you mind posting your results?

Yance
09-08-2009, 02:06 PM
Here i enclose my project example. But i'm not satisfied with this result, that's why i posting my question here to find the way for adapting my vb code in access vba. Is this possible?

Oorang
09-10-2009, 10:44 AM
It's not bad. I can still alt-f4 out of it though. And if you are really quick you can hit the windows key and then click something off the start menu. I think the answer may lie with configuring the OS properly. Take a look at this (http://www.pctools.com/guides/registry/detail/549/).

Yance
09-10-2009, 05:31 PM
I'm sorry. I forgot to post my code for prevent user to close the form/application using combination key (CTRL+F4 & ALT+F4) in Form_Unload event.
The solution on PC Tools just configure the windows registry. I also ever do that. But the way to disable the coolswitch (ALT+TAB function) with configure the registry is not working. That's why i use the LowLevelKeyboardProc function in vb to disable all the combination key.

Oorang
09-11-2009, 09:28 AM
Did you reboot after you altered the registry?

Yance
09-11-2009, 09:40 AM
Yes, i reboot my pc after that. FYI, only to disable the ALT+TAB with configure the registry is not working.

Oorang
09-11-2009, 08:53 PM
Hmmm, I haven't tried it, but there are some indications that setting these values to zero might work:
HKEY_CURRENT_USER\Control Panel\Desktop\Coolswitch
HKEY_CURRENT_USER\Control Panel\Desktop\CoolSwitchColumns
HKEY_CURRENT_USER\Control Panel\Desktop\CoolSwitchRows

Yance
09-17-2009, 11:27 PM
No, it's doesn't wok. I ever do that.

Yance
11-26-2010, 03:24 PM
Hello oorang, sorry i don't give an update for my i-cafe project about 1 year :). I've found the solution of my question to disable the hot keys, even to make/run in new desktop.

But i have one question again about: How to make our form always on top even in fullscreen game? (should i make a new post?) I already can make my access form always on top but not in all fullscreen game. In Point Blank, Counter Strike game, my form can always on top without any problem. But in a several games like Batle Realms, Pop Caps game, my form is blinking. In Call Of Duty 2, my form can't always on top (hide/in back of this game). Are you or anyone here know the solution? (sorry my bad english)

Sean.DiSanti
12-04-2010, 12:09 PM
i'm not sure how to do it through VBA, but you can accomplish all of the goals in this thread with autoit very quickly and easily. check it out at autoitscript.com/autoiot3 there's another very helpful forum over there, and if you have any questions about the language feel free to message me here or there (my name there is cameronsdad)

Yance
12-11-2010, 11:38 PM
i'm not sure how to do it through VBA, but you can accomplish all of the goals in this thread with autoit very quickly and easily. check it out at autoitscript.com/autoiot3 there's another very helpful forum over there, and if you have any questions about the language feel free to message me here or there (my name there is cameronsdad)

Can you give a sample code for using autoit in vba?