Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Disabling the keyboard

Disabling the keyboard

Scheduled Pinned Locked Moved Visual Basic
json
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dave Kreskowiak

    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

    P Offline
    P Offline
    Pasan148
    wrote on last edited by
    #4

    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.

    D 1 Reply Last reply
    0
    • P Pasan148

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #5

      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

      P 1 Reply Last reply
      0
      • D 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

        P Offline
        P Offline
        Pasan148
        wrote on last edited by
        #6

        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

        L D 2 Replies Last reply
        0
        • P Pasan148

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #7

          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.

          1 Reply Last reply
          0
          • P Pasan148

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #8

            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

            P 1 Reply Last reply
            0
            • P Pasan148

              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

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #9

              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).

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                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

                P Offline
                P Offline
                Pasan148
                wrote on last edited by
                #10

                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 IntPtr

                Private 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
                
                D 1 Reply Last reply
                0
                • P Pasan148

                  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 IntPtr

                  Private 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
                  
                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #11

                  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

                  P 1 Reply Last reply
                  0
                  • D 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

                    P Offline
                    P Offline
                    Pasan148
                    wrote on last edited by
                    #12

                    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.

                    D 1 Reply Last reply
                    0
                    • P Pasan148

                      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.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #13

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups