Capture Keydown event.

This question is answered

I was looking at the Teklogix sdk but could'nt find a way to capture Keydown event from the scanner Keyboard.

I need to be able to call a method when F1 key is pressed. I'm using C#.

thanks,

 

Verified Answer
  • moezali
    I need to be able to call a method when F1 key is pressed. I'm using C#.

    Alternatively, use the below RegisterHotKey approach

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    using Microsoft.WindowsCE.Forms;

    using System.Runtime.InteropServices;

     

    namespace RegisterHotKey

    {

       

        public partial class Form1 : Form

        {

           

            class MessageWin : MessageWindow

            {

     

    #region p/invoke

     

                [DllImport("coredll.dll")]

                public static extern bool RegisterHotKey(IntPtr hWnd,      // Window handle

                    int id,                                                                       // Hot key identifier

                    int Modifiers,                                                            // Key-modifier options

                    int key);                                                                    // Virtual-key code

     

                [DllImport("coredll.dll")]

                public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

     

    #endregion p/invoke

               

                // Event for client

                public System.EventHandler HotKeyPress;

                private int HotKeyId;

               

                public MessageWin(int id, int Modifier, int key)

                {

                    // Register to listen for hot key

                    HotKeyId = id;

                    RegisterHotKey(this.Hwnd, id, Modifier, key);

                }

     

                ~MessageWin()

                {

                    UnregisterHotKey(this.Hwnd, HotKeyId);

                }

     

                private const int WM_HOTKEY = 0x0312;

     

                protected override void WndProc(ref Message m)

                {

                    if (m.Msg == WM_HOTKEY)

                    {

                        // Raise event

                        if (HotKeyPress != null)

                            HotKeyPress(this, null);

                    }

                    base.WndProc(ref m);

                }

            }

           

            MessageWin msgWin = null;

     

            public enum KeyModifiers

            {

                None = 0,

                Alt = 1,

                Control = 2,

                Shift = 4,

                Windows = 8,

                Modkeyup = 0x1000,

            }

     

            public Form1()

            {

                InitializeComponent();

                this.Visible = false;

                msgWin = new MessageWin(0, (int)KeyModifiers.None, (int)Keys.F1);

     

                msgWin.HotKeyPress +=  new EventHandler(OnHotKeyPressEvent);

            }

     

            private void OnHotKeyPressEvent(object sender, System.EventArgs e)

            {

                MessageBox.Show("F1");

            }

        }

    }

All Replies
  • Good afternoon ,

    Which platform and OS are your targeting?

    --

    In case it helps, let me bring the below demos to your attention

    Kind regards,
    Jacques

  • Windows CE 5

  • moezali
    I need to be able to call a method when F1 key is pressed. I'm using C#.

    Does below C# code snippet help?

            public Form1()

            {

                InitializeComponent();

     

                this.KeyDown += new KeyEventHandler(OnKeyDown);

            }

     

            void OnKeyDown(object sender, KeyEventArgs e)

            {

                if (e.KeyCode == Keys.F1)

                    MessageBox.Show("F1");

            }

  • moezali
    I need to be able to call a method when F1 key is pressed. I'm using C#.

    Alternatively, use the below RegisterHotKey approach

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    using Microsoft.WindowsCE.Forms;

    using System.Runtime.InteropServices;

     

    namespace RegisterHotKey

    {

       

        public partial class Form1 : Form

        {

           

            class MessageWin : MessageWindow

            {

     

    #region p/invoke

     

                [DllImport("coredll.dll")]

                public static extern bool RegisterHotKey(IntPtr hWnd,      // Window handle

                    int id,                                                                       // Hot key identifier

                    int Modifiers,                                                            // Key-modifier options

                    int key);                                                                    // Virtual-key code

     

                [DllImport("coredll.dll")]

                public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

     

    #endregion p/invoke

               

                // Event for client

                public System.EventHandler HotKeyPress;

                private int HotKeyId;

               

                public MessageWin(int id, int Modifier, int key)

                {

                    // Register to listen for hot key

                    HotKeyId = id;

                    RegisterHotKey(this.Hwnd, id, Modifier, key);

                }

     

                ~MessageWin()

                {

                    UnregisterHotKey(this.Hwnd, HotKeyId);

                }

     

                private const int WM_HOTKEY = 0x0312;

     

                protected override void WndProc(ref Message m)

                {

                    if (m.Msg == WM_HOTKEY)

                    {

                        // Raise event

                        if (HotKeyPress != null)

                            HotKeyPress(this, null);

                    }

                    base.WndProc(ref m);

                }

            }

           

            MessageWin msgWin = null;

     

            public enum KeyModifiers

            {

                None = 0,

                Alt = 1,

                Control = 2,

                Shift = 4,

                Windows = 8,

                Modkeyup = 0x1000,

            }

     

            public Form1()

            {

                InitializeComponent();

                this.Visible = false;

                msgWin = new MessageWin(0, (int)KeyModifiers.None, (int)Keys.F1);

     

                msgWin.HotKeyPress +=  new EventHandler(OnHotKeyPressEvent);

            }

     

            private void OnHotKeyPressEvent(object sender, System.EventArgs e)

            {

                MessageBox.Show("F1");

            }

        }

    }