Disabling the keyboard
-
Is there a way to avoid typing alphabetic keys (A-Z) in any window and track the key which was pressed at same time. I tried to use BlockInput API. But it disabled all keys in keyboard. I hope your kind reply. Thanks
I have to ask why you would want to do such a thing. I can't see any useful purpose in it. The easiest way to do it would be to create your own TextBox control (inheriting from TextBox) and handling the Key/Press/Down/Up events, searching for the keys that are allowed and passing them on to the base TextBox. Anything else you would do whatever you needed with them and just not pass them down to the base TextBox.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Is there a way to avoid typing alphabetic keys (A-Z) in any window and track the key which was pressed at same time. I tried to use BlockInput API. But it disabled all keys in keyboard. I hope your kind reply. Thanks
Pasan148 wrote:
Is there a way to avoid typing alphabetic keys (A-Z) in any window and track the key which was pressed at same time.
Just as a warning; you'd be violating the law if this application would be running on a PC in the Netherlands without the users' prior consent.
I are Troll :suss:
-
I have to ask why you would want to do such a thing. I can't see any useful purpose in it. The easiest way to do it would be to create your own TextBox control (inheriting from TextBox) and handling the Key/Press/Down/Up events, searching for the keys that are allowed and passing them on to the base TextBox. Anything else you would do whatever you needed with them and just not pass them down to the base TextBox.
A guide to posting questions on CodeProject[^]
Dave KreskowiakMy requirement is to create a transliteration application. If I do this as you suggested, its ability limits to one application. But I want allow users to do transliteration on any window. To do so first I want to avoid typing English letters and track the key which was pressed. Later it will send translated letter to the active window. That's what I want.
-
My requirement is to create a transliteration application. If I do this as you suggested, its ability limits to one application. But I want allow users to do transliteration on any window. To do so first I want to avoid typing English letters and track the key which was pressed. Later it will send translated letter to the active window. That's what I want.
The onyl way to do that would be a global keyboard hook. The problem is you get keyboard scancodes, not letters. You'll have to map the key to the letter you want, see if it's allowed, and if so, send the key message down to the next hook in the chain. If not, you'll have to find the letter that is allowed, map that back to a scancode, alter the keyboard message and send that down the hook chain.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
The onyl way to do that would be a global keyboard hook. The problem is you get keyboard scancodes, not letters. You'll have to map the key to the letter you want, see if it's allowed, and if so, send the key message down to the next hook in the chain. If not, you'll have to find the letter that is allowed, map that back to a scancode, alter the keyboard message and send that down the hook chain.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
As I know, .NET Framework doesn't support global hooks and I am not familiar with hooks . Can you suggest another possible way to do this with .NET
What Dave says goes; if you don't like it that's too bad. :|
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
As I know, .NET Framework doesn't support global hooks and I am not familiar with hooks . Can you suggest another possible way to do this with .NET
Pasan148 wrote:
As I know, .NET Framework doesn't support global hooks
Actually, you're wrong. Global Mouse and Keyboard hooks, among a couple of others, work just fine in .NET. You just have to Google for examples.
Pasan148 wrote:
Can you suggest another possible way to do this with .NET
There is no other way.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Is there a way to avoid typing alphabetic keys (A-Z) in any window and track the key which was pressed at same time. I tried to use BlockInput API. But it disabled all keys in keyboard. I hope your kind reply. Thanks
When trying to transliterate the input, you should also know that the keyboard layout is quite different for each set of characters. And some languages use many more characters than does English, many write syllables (Japanese, Indian alphabets including Thai and Khmer) or words (Chinese, Japanese).
-
Pasan148 wrote:
As I know, .NET Framework doesn't support global hooks
Actually, you're wrong. Global Mouse and Keyboard hooks, among a couple of others, work just fine in .NET. You just have to Google for examples.
Pasan148 wrote:
Can you suggest another possible way to do this with .NET
There is no other way.
A guide to posting questions on CodeProject[^]
Dave KreskowiakI used following code to implement my project as you suggest
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYUP As Integer = &H105
Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
Private hookID As IntPtrPrivate Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, \_ ByVal lParam As IntPtr) As IntPtr <DllImport("user32")> \_ Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, \_ ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr End Function <DllImport("user32.dll")> \_ Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <DllImport("user32.dll")> \_ Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, \_ ByVal lParam As IntPtr) As IntPtr End Function <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> \_ Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr End Function Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. hookID = SetHook(proc) End Sub Private Sub Form1\_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing UnhookWindowsHookEx(hookID) End Sub Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr Using curProcess As Process = Process.GetCurrentProcess() Using curModule As ProcessModule = curProcess.MainModule Return SetWindowsHookEx(WH\_KEYBOARD\_LL, proc, GetModuleHandle(curModule.ModuleName), 0) End Using End Using End Function
But there is a problem in this function
Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
If nCode Then Dim vkCode As Integer = Marshal.ReadInt32(lParam) If vkCode = Keys.A Then SendKeys.Send("B") Return CType(1, IntPtr) End If End If Return CallNextHookEx(hookID, nCode, wParam, lParam) End Fu
-
I used following code to implement my project as you suggest
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYUP As Integer = &H105
Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
Private hookID As IntPtrPrivate Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, \_ ByVal lParam As IntPtr) As IntPtr <DllImport("user32")> \_ Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, \_ ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr End Function <DllImport("user32.dll")> \_ Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <DllImport("user32.dll")> \_ Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, \_ ByVal lParam As IntPtr) As IntPtr End Function <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> \_ Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr End Function Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. hookID = SetHook(proc) End Sub Private Sub Form1\_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing UnhookWindowsHookEx(hookID) End Sub Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr Using curProcess As Process = Process.GetCurrentProcess() Using curModule As ProcessModule = curProcess.MainModule Return SetWindowsHookEx(WH\_KEYBOARD\_LL, proc, GetModuleHandle(curModule.ModuleName), 0) End Using End Using End Function
But there is a problem in this function
Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
If nCode Then Dim vkCode As Integer = Marshal.ReadInt32(lParam) If vkCode = Keys.A Then SendKeys.Send("B") Return CType(1, IntPtr) End If End If Return CallNextHookEx(hookID, nCode, wParam, lParam) End Fu
It's doing that because you used SendKeys n a keyboard hook. Seriously, read up on what you're doing with a keyboard hook. When you callback function is, well, called, you are given the keyscan code before the system gets the key. This is your opportunity to MODIFY THE MESSAGE before it gets to the system. Remove the SendKeys line and replace it with a line that sets a new value for lParam with the appropriate key you want.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
It's doing that because you used SendKeys n a keyboard hook. Seriously, read up on what you're doing with a keyboard hook. When you callback function is, well, called, you are given the keyscan code before the system gets the key. This is your opportunity to MODIFY THE MESSAGE before it gets to the system. Remove the SendKeys line and replace it with a line that sets a new value for lParam with the appropriate key you want.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Please can you show me the code to set a new value for lParam variable. Since I am not familiar with hooks I can't understand how to do that.
All you're doing is converting the new keycode to an Integer and passing the new value to the CallNextHookEx function. Seriously, why is this so hard?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak