-1

I try to get KeyEventArgs to Tick. If someone thinks why I'm using timer, so it needs to detect keypress outside of window too.

I don't have "error" in this code, but when I run program comes:

Exception Unhandled. System.NullReferenceExpetion: (i try to translate) 'Your object referral can't define object occurrence'

Now thing what I'm using is

private void Form1_Load(object sender, EventArgs e)
        {
            A.Start();
            A.Interval = 1;
        }

private void A_Tick(object sender, EventArgs e)
        {
            KeyEventArgs ke = e as KeyEventArgs;
            if (ke.KeyCode == Keys.R) 
            {
            test = true;     
            }       
        }
            

I really need help, because this error has been so long time. Thanks

  • `ok` is probably null and accessing `ok.KeyCode` is probably throwing the null reference exception. `EventArgs e` can't be cast to `KeyEventArgs` for some reason most likely. Also, what are you trying to do here? Why do you have KeyEventArgs in a tick event? – KSib Aug 23 '17 at 13:28
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dmitry Aug 23 '17 at 13:28
  • 1
    What triggers A_Tick(object sender, EventArgs e) – John Aug 23 '17 at 13:29
  • John Form load runs A_Tick. –  Aug 23 '17 at 13:31
  • Possible duplicate: https://stackoverflow.com/questions/19171081/how-can-i-capture-a-key-press-outside-of-the-form – BFlat Aug 23 '17 at 13:45

1 Answers1

1

From looking at the way you put your question together to me it seems you're trying to detect a key press using a timer. Instead you can use the Form's KeyPress event.

Use:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyCode == Keys.SOMETHING)
    {
        //do something
    }
}

Don't forget to set KeyPreview = true; for the Form.

EDIT: If you need to detect keypresses even without focus(outside of the form), you need a global hotkey by hooking. You need the following:

First include this:

using System.Runtime.InteropServices;

You need this at very top of your class:

[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);
const int HOTKEY = 1;

And you need to call this inside Form's Load:

RegisterHotKey(this.Handle, HOTKEY, (uint)ModifierKeys.SOMETHING, (uint) Keys.SOMETHING);

Then you need to handle the press:

protected override void WndProc(ref Message m) 
{
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == HOTKEY) 
    {
        //do something when pressed
    }
    base.WndProc(ref m);
}
Hubbs
  • 163
  • 11
  • Does this work outside of form? Because it need to work. –  Aug 23 '17 at 13:39
  • @HappyFrog: What, exactly, are you trying to do? – willaien Aug 23 '17 at 13:41
  • @HappyFrog I've updated my answer with an EDIT included that demonstrates how to detect keypresses outside of the form (not focused). – Hubbs Aug 23 '17 at 13:53
  • What I put to: RegisterHotKey(this.Handle, HOTKEY, (uint)ModifierKeys.**HERE**, (uint) because it does not accept key. –  Aug 23 '17 at 14:19
  • @HappyFrog If you don't want modifierkeys then use ModifierKeys.None. – Hubbs Aug 23 '17 at 14:31
  • Nope. Comes error if i put there anything. Error says "Member 'Keys.None' cannot be accessed with an instance reference; qualify it with a type name instead" –  Aug 23 '17 at 14:40