none
Catch Ctrl+A Global Hook

    Question

  • Hi,

    I using code from "http://www.codeproject.com/KB/cs/globalhook.aspx"

    and i don't know how to catch "ctrl+a",

    something similar:

    Question

    How do I catch key combinations like Ctrl+Shift+A?

    Answer

    You'll have to track which keys have gone down but not up. Only the most recently pressed key keeps sending KeyDown messages, but the others will still send a KeyUp when released. So if you make three flags IsCtrlDown , IsShiftDown , and IsADown , and set them to true at KeyDown and false at KeyUp , the expression (IsCtrlDown && IsShiftDown && IsADown) will give you the required result.

     

    but how to do that :( sorry i just beginner ... and thx for help...

    Saturday, February 12, 2011 8:05 PM

Answers

  • This might help:

      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
          if (e.Control && e.KeyCode == Keys.A)
            MessageBox.Show("you have pressed Ctrl + A");
          else if (e.Control && e.Shift && e.KeyCode == Keys.A)
            MessageBox.Show("You have pressed Ctrl + Shift + A");
        }
      }
    

    • Proposed as answer by Mio_Miao Thursday, February 24, 2011 3:35 AM
    • Marked as answer by Mio_Miao Friday, March 04, 2011 4:02 AM
    Saturday, February 12, 2011 9:46 PM

All replies

  • If all you want is to be notified when the user hits a key combination, then I recommend you simply forget global keyboard hooks and use the RegisterHotKey & UnregisterHotKey Windows API functions.  I'm sure there are plenty of examples out there on how to do it in C#.

    ShaneB

    Saturday, February 12, 2011 8:38 PM
  • This might help:

      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
          if (e.Control && e.KeyCode == Keys.A)
            MessageBox.Show("you have pressed Ctrl + A");
          else if (e.Control && e.Shift && e.KeyCode == Keys.A)
            MessageBox.Show("You have pressed Ctrl + Shift + A");
        }
      }
    

    • Proposed as answer by Mio_Miao Thursday, February 24, 2011 3:35 AM
    • Marked as answer by Mio_Miao Friday, March 04, 2011 4:02 AM
    Saturday, February 12, 2011 9:46 PM
  • Ooops!  I thought he was looking for a system-wide CTRL-A key press based on the CodeProject link he posted.  If not, then your method is obviously the simplest :)

    ShaneB

    Saturday, February 12, 2011 10:25 PM