How do you disable the alt key?????
Hi, i'm new at this, how would you stop someone using the alt key, like alt+F4, alt+ctrl+delete OR alt+tab
Ответы
Hi,
I hope that this will get you started; watch for word wrap:
Code SnippetImports
System.Runtime.InteropServicesImports
System.ReflectionModule
mKeyboard#
Region "Declarations" Private Const HC_ACTION As Integer = 0 Private Const WH_KEYBOARD_LL As Integer = 13&Private Structure KBDLLHookStruct
Public vkCode As Integer
Public scanCode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As KeyboardHookDelegate Private mbBlockKeys As Boolean = True Private miKeyboardHandle As Integer = 0Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
Private Declare Function CallNextHookEx Lib "user32" ( _ByVal hHook As Integer, ByVal nCode As Integer, _
ByVal wParam As Integer, ByVal lParam As KBDLLHookStruct) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" _Alias "SetWindowsHookExA" _
(ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _ByVal hHook As Integer) As Integer
#
End Region#
Region "Properties" Public Property BlockKeyCombinations() As Boolean Get Return mbBlockKeys End Get Set(ByVal value As Boolean)mbBlockKeys = value
End Set End Property
Public ReadOnly Property IsHooked() As Boolean Get Return miKeyboardHandle <> 0 End Get End Property#
End Region
Public Function IsInIDE() As Boolean Return System.Diagnostics.Debugger.IsAttached End Function
Public Sub HookKeyboard() ' Release any existing keyboard hook.UnhookKeyboard()
If Not IsInIDE() Thencallback =
New KeyboardHookDelegate(AddressOf KeyboardCallback)miKeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, _
Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End If End Sub
Public Sub UnhookKeyboard() If (IsHooked()) Then Call UnhookWindowsHookEx(miKeyboardHandle) End Sub
Private Function BlockKeyCombination( _ByVal Hookstruct As KBDLLHookStruct) As Boolean
Dim bResult As Boolean = False
If mbBlockKeys Then Select Case Hookstruct.vkCode Case System.ConsoleKey.Escape If My.Computer.Keyboard.CtrlKeyDown ThenDebug.Print(
"Blocking: Ctrl-Esc")bResult =
True ElseIf My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-Esc")bResult =
True End If Case System.ConsoleKey.Tab If My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-Tab")bResult =
True End If Case System.ConsoleKey.RightWindows, System.ConsoleKey.LeftWindowsDebug.Print(
"Blocking: Windows Key")bResult =
True Case System.ConsoleKey.ApplicationsDebug.Print(
"Blocking: Application Key")bResult =
True Case System.ConsoleKey.F4 If My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-F4")bResult =
True End If Case Else End Select End If
Return bResult End Function
Private Function KeyboardCallback(ByVal Code As Integer, _ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
Dim lResult As Integer = 0 If (Code = HC_ACTION) AndAlso (BlockKeyCombination(lParam)) ThenlResult = 1
ElselResult = CallNextHookEx(miKeyboardHandle, Code, wParam, lParam)
End If
Return lResult End FunctionEnd
Module
Все ответы
I'll tell you, but only if you PROMISE you aren't going to write an app which is so so self-important that it thinks it must be always on top
Hi,
I hope that this will get you started; watch for word wrap:
Code SnippetImports
System.Runtime.InteropServicesImports
System.ReflectionModule
mKeyboard#
Region "Declarations" Private Const HC_ACTION As Integer = 0 Private Const WH_KEYBOARD_LL As Integer = 13&Private Structure KBDLLHookStruct
Public vkCode As Integer
Public scanCode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As KeyboardHookDelegate Private mbBlockKeys As Boolean = True Private miKeyboardHandle As Integer = 0Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
Private Declare Function CallNextHookEx Lib "user32" ( _ByVal hHook As Integer, ByVal nCode As Integer, _
ByVal wParam As Integer, ByVal lParam As KBDLLHookStruct) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" _Alias "SetWindowsHookExA" _
(ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _ByVal hHook As Integer) As Integer
#
End Region#
Region "Properties" Public Property BlockKeyCombinations() As Boolean Get Return mbBlockKeys End Get Set(ByVal value As Boolean)mbBlockKeys = value
End Set End Property
Public ReadOnly Property IsHooked() As Boolean Get Return miKeyboardHandle <> 0 End Get End Property#
End Region
Public Function IsInIDE() As Boolean Return System.Diagnostics.Debugger.IsAttached End Function
Public Sub HookKeyboard() ' Release any existing keyboard hook.UnhookKeyboard()
If Not IsInIDE() Thencallback =
New KeyboardHookDelegate(AddressOf KeyboardCallback)miKeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, _
Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End If End Sub
Public Sub UnhookKeyboard() If (IsHooked()) Then Call UnhookWindowsHookEx(miKeyboardHandle) End Sub
Private Function BlockKeyCombination( _ByVal Hookstruct As KBDLLHookStruct) As Boolean
Dim bResult As Boolean = False
If mbBlockKeys Then Select Case Hookstruct.vkCode Case System.ConsoleKey.Escape If My.Computer.Keyboard.CtrlKeyDown ThenDebug.Print(
"Blocking: Ctrl-Esc")bResult =
True ElseIf My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-Esc")bResult =
True End If Case System.ConsoleKey.Tab If My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-Tab")bResult =
True End If Case System.ConsoleKey.RightWindows, System.ConsoleKey.LeftWindowsDebug.Print(
"Blocking: Windows Key")bResult =
True Case System.ConsoleKey.ApplicationsDebug.Print(
"Blocking: Application Key")bResult =
True Case System.ConsoleKey.F4 If My.Computer.Keyboard.AltKeyDown ThenDebug.Print(
"Blocking: Alt-F4")bResult =
True End If Case Else End Select End If
Return bResult End Function
Private Function KeyboardCallback(ByVal Code As Integer, _ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer
Dim lResult As Integer = 0 If (Code = HC_ACTION) AndAlso (BlockKeyCombination(lParam)) ThenlResult = 1
ElselResult = CallNextHookEx(miKeyboardHandle, Code, wParam, lParam)
End If
Return lResult End FunctionEnd
ModuleJames,
I am having a very similar issue to Simone1. I will try this out and see if it addresses the problem.
Thank you for the help...
James,
I got the code into my program, but what do I do with it? What do I call/pass so that input is checked?
I have it as a module available to the whole program. I can call the HookKeyboard public sub - but when I debug the code, pressing the keys that your code should trap, nothing happens...
Hi,
Call mKeyboard.HookKeyboard in the application startup.Call mKeyboard.UnhookKeyboard when shutting down.
Change mKeyboard.BlockKeyCombination to disallow key combinations.
Set mKeyboard.BlockKeyCombinations to True to block the defined key combinations.
That should get you going. Keep in mind that HookKeyboard will not install a new keyboard hook when running in Debug mode. Try running in release mode.
If you are still stuck, please let me know.
kandaras0 wrote: Hi, i'm new at this, how would you stop someone using the alt key, like alt+F4, alt+ctrl+delete OR alt+tab
You can perhaps aslo use the SystemParametersInfo API here. This API will block all these keys ( except for Alt + Ctrl + Del ). The reaon for this is that whether or not you specify it, the Task manager ( on Windows XP ) will still show.
That will be your biggest headache.
You can disable the Taskmanager by adding a Timer to your Form, and keep bringing your form to the fron ( ok, it won't disable tha taskmanager, but it will hide it )
To really disable the Taskmanager from showing by editing the registry key :
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType
or you could get the Taskmanager's Process Id with the GetWindowThreadProcessId API, then terminate it with the TerminateProcess API.
To be honest. I'm not if any of my comments were illegal in this forum, so mods, feel free to edit / delet as you see fit.
Another comment kandaras, is that it is very impractical and illogical why anyone would need to hide the taskmanager completely. The Task Manager exists for a reason.
HanneSThEGreaT wrote: Another comment kandaras, is that it is very impractical and illogical why anyone would need to hide the taskmanager completely. The Task Manager exists for a reason.
I agree that most applications should not prevent Task Manager from popping up nor should they block key combinations. Instead, policies are probably a better way to go. In my particular case, the applications I write control machinery. Forcibly killing the application or doing other tasks in Windows could cause machine malfunctions. Since we do not always have have access to change policies, it was easier/quicker to disable "stuff" within the application. That being said, we do allow that ability to be configured by user access (operators can't exit the app, but admins can for example).
I am, however, always open to different solutions.
- Hello James,
I had to change the visibility of BlockKeyCombination to Public because i couldn't call it from my window.
Also, when i call the BlockKeyCombination function, no parameters pulls out thru IntelliSense.
What should i specify in the KBDLLHookStruct structure ?
I have seen this :http://msdn.microsoft.com/en-us/library/ms644967(VS.85).aspx
KBDLLHOOKSTRUCT Structure
But it doesn't tell what values are for the desired key ... etc
Help please,
Thank you !

