0

My application is able to detect if the shift key is pressed but only for the first modified key. The second modified key(even when shift is held down) is detected as unmodified even while the key is still held.

I want to be able to properly detect if the key is modified by shift after the first modified key(e.g. for second, third and so on..)

Code for registering is as follows :

[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

public Form1()
    {
        InitializeComponent();
        RegisterHotKey(this.Handle, 1, 0, (int)Keys.A);
        RegisterHotKey(this.Handle, 101, 4, (int)Keys.A);
    }

Event handler(when the keys are pressed) :

protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            switch (m.WParam.ToInt32())
            {
                case 1:
                    //add to a textbox
                    keypresstext.AppendText("a");
                    //unregister so the app doesnt rerecord it resulting in a loop
                    UnregisterHotKey(this.Handle, 1);
                    //send the key after intercepting
                    SendKeys.SendWait("a");
                    //reregister
                    RegisterHotKey(this.Handle, 1, 0,(int)Keys.A);
                    break;
                case 101:
                    keypresstext.AppendText("A");
                    UnregisterHotKey(this.Handle, 101);
                    SendKeys.Send("+{a}");
                    RegisterHotKey(this.Handle, 101, 4, (int)Keys.A);
                    break;
             }
        }
        base.WndProc(ref m);
    }
Orphamiel
  • 829
  • 11
  • 21
  • 1
    Not sure, but maybe you could just handle Shift keyup and keydown separately and keep some state so that your app knows if its currently held down or not when other keys are pressed. – mclaassen Jun 20 '14 at 19:54
  • Great idea although not the most optimal solution I was looking for. Thanks for the idea – Orphamiel Jun 20 '14 at 20:05
  • 1
    This sounds like you're trying to make your proposed solution work instead of solving your real problem. You will get better answers if you described the problem you are trying to solve. – IInspectable Jun 20 '14 at 20:57
  • Good point thanks. Edited original post – Orphamiel Jun 20 '14 at 21:00
  • Just a guess, but is the unregister/re-register code messing with the state? I'd add a variable indicating you're currently handling a hotkey and change the if to "if (m.Msg == 0x312 && !fHandlingThisAlready)" – HerrJoebob Jun 20 '14 at 22:12
  • Its stuck an I/O loop using your method @HerrJoebob. Thanks for trying – Orphamiel Jun 21 '14 at 17:06

0 Answers0