0

I'm currently trying to make a keydown register. Basicly just to see which keys i use the most. The problem is that only want it to detect the F1-F12 buttons. Another issue is don't really know how to attack it since it must be a globalevent.

if (e.KeyCode.ToString() == "F1")
{
    MessageBox.Show("F1 pressed");
}

Is what I've been trying so far, I do have to focus the application for it to work tho.

I DO NOT want the user to register the hotkeys on their own. I want them set, that's what differs this from Set global hotkeys using C#

Community
  • 1
  • 1
Hajpz
  • 41
  • 8
  • You want it to work globally? I mean, on every application and not just yours? – Nasreddine Oct 20 '15 at 10:34
  • search for low level keyboard hook. also there is a solution in codeproject. i think thats what you want. go in first search result https://www.google.com/search?q=low+leve+key+press+event+c%23&ie=utf-8&oe=utf-8 – M.kazem Akhgary Oct 20 '15 at 10:43
  • http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx – Marcus Oct 20 '15 at 10:44
  • 1
    Possible duplicate of [Set global hotkeys using C#](http://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp) – Wiktor Stribiżew Oct 20 '15 at 10:47
  • I tried hitting it with ProcessCMDKey instead, which only resolved in Error: CS0161 'form1.ProcessCmdKey(ref Message, Keys)': not all code paths return a value – Hajpz Oct 20 '15 at 12:33

3 Answers3

0

When I need to do this in C#, I just import User32.dll, and utilize the GetASyncKeyState function as follows:

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);

private void globalKeyThread()
{
   int state;
   System.Windows.Forms.Keys keyToCheck = Keys.Space;

   while (true)
   {
      state = Convert.ToInt32(GetAsyncKeyState(keyToCheck));

      if (state == -32767)
      {
         // Handle what happens when key is pressed.
      }

      Thread.Sleep(10);
   }
}
Shane Duffy
  • 1,117
  • 8
  • 18
  • No errors, when i use that however the application does not seem to get that i press space. – Hajpz Oct 20 '15 at 12:50
0
private void globalKeyThread()
        {
            System.Windows.Forms.Keys keyToCheck = Keys.F12;

            while (true)
            {
                state = Convert.ToInt32(GetAsyncKeyState(keyToCheck));

                if (state == -32767)
                {
                    // Handle what happens when key is pressed.
                    timer1.Start();
                }

                Thread.Sleep(10);
            }
        }

The issue right here might be the Int32 response perhaps? Since the application is not really understanding what i'm trying to do.

Hajpz
  • 41
  • 8
-1
using System.Runtime.InteropServices;

Import User32.dll:

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);

Next create function will check that key is pressed:

private bool globalKeyThread(Keys key)
{
    int state;
    state = Convert.ToInt32(GetAsyncKeyState(key));
    if (state == -32767)
    {
        return true;
    }
    return false;
}

Now function for timer is only for F1:

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Enabled = false;

    if (globalKeyThread(Keys.F1))
    {
        MessageBox.Show("F1 press");
    }
    timer1.Enabled = true;
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77