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. .NET (Core and Framework)
  4. Kill key strokes while hooked

Kill key strokes while hooked

Scheduled Pinned Locked Moved .NET (Core and Framework)
11 Posts 4 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 Offline
    D Offline
    digiwombat
    wrote on last edited by
    #1

    Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006

    G M 2 Replies Last reply
    0
    • D digiwombat

      Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      You just figured it out? So, what is your problem? :) -- modified at 19:23 Friday 17th February, 2006

      D 1 Reply Last reply
      0
      • G George L Jackson

        You just figured it out? So, what is your problem? :) -- modified at 19:23 Friday 17th February, 2006

        D Offline
        D Offline
        digiwombat
        wrote on last edited by
        #3

        Just *can't* figure out. Why is it that we always leave out the mosti mportant words? haha.

        G 1 Reply Last reply
        0
        • D digiwombat

          Just *can't* figure out. Why is it that we always leave out the mosti mportant words? haha.

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          This might help: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx[^] static void Main() { Application.AddMessageFilter(new TestMessageFilter()); Application.Run(new Form1()); } public class TestMessageFilter : IMessageFilter { public bool PreFilterMessage(ref Message m) { const int WM_KEYDOWN= 0x0100; // Blocks all the messages relating to the left mouse button. if (m.Msg==WM_KEYDOWN) { MessageBox.Show("KeyDown hitted"); } return false; } } -- modified at 19:54 Friday 17th February, 2006

          1 Reply Last reply
          0
          • D digiwombat

            Ok, I am using a global hook, and I am wondering how I can keep the keys from sending to the active program. I searched around the net and CodeProject and I just can't figure it out. I'm not terribly familiar with WindowsAPI stuff. Thanks in advance. -- modified at 19:33 Friday 17th February, 2006

            M Offline
            M Offline
            Marc 0
            wrote on last edited by
            #5

            What type of hook are you using? A really global hook that captures all keyboard events from the whole system, or a hook that only applies to one application? Maybe you can show us a bit of code?


            "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


            || Fold With Us! || Pensieve || VG.Net ||

            D 1 Reply Last reply
            0
            • M Marc 0

              What type of hook are you using? A really global hook that captures all keyboard events from the whole system, or a hook that only applies to one application? Maybe you can show us a bit of code?


              "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


              || Fold With Us! || Pensieve || VG.Net ||

              D Offline
              D Offline
              digiwombat
              wrote on last edited by
              #6

              It is a global keyboard hook. I am using the Kennedy.ManagedHooks DLL from this site. I enable it with a hotkey, and disable it the same way. public Form1() { // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 Form1.RegisterHotKey(this.Handle,this.GetType().GetHashCode(), 8, (int)'P'); InitializeComponent(); keyboardHook = new Kennedy.ManagedHooks.KeyboardTracking(); keyboardHook.KeyboardEvent += new Kennedy.ManagedHooks.KeyboardTracking.KeyboardEventHandler(keyboardHook_KeyboardEventExt); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) if (keyboardHook.IsHooked) { keyboardHook.UninstallHook(); input = false; textBox4.Text = ""; } else { textBox4.Text = ""; keyboardHook.InstallHook(); input = true; } base.WndProc(ref m); } private void keyboardHook_KeyboardEventExt(Kennedy.ManagedHooks.KeyboardEvents kEvent, Keys key) { bool shi; if(keyboardHook.ShiftPressed == true) shi = true; else shi = false; if(kEvent == KeyboardEvents.KeyDown){ string msg; //snipped out some key parsing junk. if(shi == true) msg = key.ToString(); else msg = key.ToString().ToLower(); textBox4.Text += msg; } } I think that covers all the relevant code from my end of the application. If there are any questions about what anything does more specifically, I can answer those. The Hotkey works fine, and I am planning to add the message filter or whatever when it hotkeys in and remove it when they hotkey out of the hook, obviously. Thanks for the help thus far.

              D 1 Reply Last reply
              0
              • D digiwombat

                It is a global keyboard hook. I am using the Kennedy.ManagedHooks DLL from this site. I enable it with a hotkey, and disable it the same way. public Form1() { // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 Form1.RegisterHotKey(this.Handle,this.GetType().GetHashCode(), 8, (int)'P'); InitializeComponent(); keyboardHook = new Kennedy.ManagedHooks.KeyboardTracking(); keyboardHook.KeyboardEvent += new Kennedy.ManagedHooks.KeyboardTracking.KeyboardEventHandler(keyboardHook_KeyboardEventExt); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) if (keyboardHook.IsHooked) { keyboardHook.UninstallHook(); input = false; textBox4.Text = ""; } else { textBox4.Text = ""; keyboardHook.InstallHook(); input = true; } base.WndProc(ref m); } private void keyboardHook_KeyboardEventExt(Kennedy.ManagedHooks.KeyboardEvents kEvent, Keys key) { bool shi; if(keyboardHook.ShiftPressed == true) shi = true; else shi = false; if(kEvent == KeyboardEvents.KeyDown){ string msg; //snipped out some key parsing junk. if(shi == true) msg = key.ToString(); else msg = key.ToString().ToLower(); textBox4.Text += msg; } } I think that covers all the relevant code from my end of the application. If there are any questions about what anything does more specifically, I can answer those. The Hotkey works fine, and I am planning to add the message filter or whatever when it hotkeys in and remove it when they hotkey out of the hook, obviously. Thanks for the help thus far.

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

                Well, in the "real" global hook implementation I did in C++, years ago, the solution is simple. You just don't pass the keystroke message up the hook chain. But I don't see a way to do that, or more to the point NOT do that, using this library. At least it's not obvious in the code you've posted. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Well, in the "real" global hook implementation I did in C++, years ago, the solution is simple. You just don't pass the keystroke message up the hook chain. But I don't see a way to do that, or more to the point NOT do that, using this library. At least it's not obvious in the code you've posted. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  D Offline
                  D Offline
                  digiwombat
                  wrote on last edited by
                  #8

                  Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.

                  D D 2 Replies Last reply
                  0
                  • D digiwombat

                    Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.

                    D Offline
                    D Offline
                    digiwombat
                    wrote on last edited by
                    #9

                    Ok, I am just going to use blockinput() I think. It blocks the mouse too, but oh well.

                    1 Reply Last reply
                    0
                    • D digiwombat

                      Also, I am using WH_KEYBOARD_LL to hook, if that helps out more.

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

                      All you need to do to eat the key message is NOT call the CallNextHookEx function in your KeyboardProc handler. It's that simple... LowLevelKeyboardProc Function[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 9:33 Saturday 18th February, 2006

                      D 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        All you need to do to eat the key message is NOT call the CallNextHookEx function in your KeyboardProc handler. It's that simple... LowLevelKeyboardProc Function[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 9:33 Saturday 18th February, 2006

                        D Offline
                        D Offline
                        digiwombat
                        wrote on last edited by
                        #11

                        Thanks! I'll go mess around with my hooking code.

                        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