none
Waiting for any key to be pressed then returning that key (C# Windows Form App) RRS feed

  • Question

  • So I'm trying to make an application that automatically types when the user presses a key that they can set. I don't know how I would go about storing this key even after the form is restarted but this is kind of what the code I'm looking for to be like:

    label1_clicked_event {
        start_looking_for_next_key;
        key = the_key_that_was_Pressed;
        save_key_after_reloading_form
        
        if (no_key_pressed_within(10 seconds))
        {
            stop_looking_for_key;
            label1.Text = "None";
        }
        
        
        //Example of how the key can be used
        if (key == "Escape")
        {
            label1.Text = "None";
        }
    }
    
    timer1 {
        test_if_a_key_is_pressed_even_if_window_isn't_active
        if (key_pressed == key)
        {
            start_typing
        }
    }

    Wednesday, August 21, 2019 1:19 AM

Answers

  • Hi,

    To get the input key and show it in a label, you can use the event "KeyDown".

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyCode.ToString();
        }

    As to store the label text, you can use "Settings" to achieve it. Here is the steps.

    1.Click "Project" and choose "Properties…", then choose "Settings".

    2.Add new setting in "Settings"

    3.Try the following code:

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = Properties.Settings.Default.labeltext;
        }
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyCode.ToString();
            Properties.Settings.Default.labeltext = label1.Text;
            Properties.Settings.Default.Save();
        }

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Lightcaster5 Wednesday, August 21, 2019 8:35 PM
    Wednesday, August 21, 2019 5:28 AM
    Moderator

All replies

  • Hi,

    To get the input key and show it in a label, you can use the event "KeyDown".

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyCode.ToString();
        }

    As to store the label text, you can use "Settings" to achieve it. Here is the steps.

    1.Click "Project" and choose "Properties…", then choose "Settings".

    2.Add new setting in "Settings"

    3.Try the following code:

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = Properties.Settings.Default.labeltext;
        }
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyCode.ToString();
            Properties.Settings.Default.labeltext = label1.Text;
            Properties.Settings.Default.Save();
        }

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Lightcaster5 Wednesday, August 21, 2019 8:35 PM
    Wednesday, August 21, 2019 5:28 AM
    Moderator
  • Thanks! It worked just how I wanted it to... I do have a few more questions

    How can I make the hotkey usable even if the window isn't active

    How do I reset the "Properties.Settings.Default.labeltext", is there something at the end I put like .reset or just = "";

    Wednesday, August 21, 2019 8:37 PM
  • Hi Lightcaster5,

    >> How can I make the hotkey usable even if the window isn't active

    Maybe you can try hook "SetWindowsHookEx()" to achieve it. Here is the simple demo.

    Code in Form1.cs:

        // Create property to access label1
        public Label label
        {
            get { return label1; }
            set { label1 = value; }
        }

    Code in Program.cs:

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            _hookID = SetHook(_proc);
            Form1 form1 = new Form1();
            Application.Run(form1);
            UnhookWindowsHookEx(_hookID);
        }
    
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int VK_F1 = 0x70;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
    
        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }
    
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                Keys number = (Keys)Marshal.ReadInt32(lParam);
                Form1 form = (Form1)Application.OpenForms["Form1"];
                form.label.Text = number.ToString();
            }
            return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

    >> How do I reset the "Properties.Settings.Default.labeltext"

    If you want to reset a specific setting, you could use

        Properties.Settings.Default.labeltext = string.Empty;

    If you want to reset all settings, you could use

        Properties.Settings.Default.Reset();

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 22, 2019 2:41 AM
    Moderator
  • Thanks! It worked just how I wanted it to!
    Friday, August 23, 2019 7:24 PM