How to get a desired key from user within a TextBox and use it in Keyboard.isKeyDown(Key." ... ")

Viewed 140

I just start C# and I wanted to make a Form App which gets a key from user, within the TextBox, then somehow bind that key and with SendKeys(), spam a specific key that I set in codes e.g(e), I use this code but the Keyboard.isKeyDown(Key.key) wants me an enum of "Key" and TexBox returns a string and even I converted the string to an enum but it just want me a key from its own Key enum, my question is how to pass a variable key to the Keyboard.isKeyDown(Key.key) maybe totally my way is not right, can you help me with these codes or suggest another way to make this app?

Here's the code:

using System;
using System.Windows.Forms;
using System.Windows.Input;
using System.Threading;
using System.Runtime.InteropServices;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread TH = new Thread(PressBind);
            TH.SetApartmentState(ApartmentState.STA);
            CheckForIllegalCrossThreadCalls = false;
            TH.Start();
        }

        //i want to get the key from this textbox

        public void TxBxKTB_TextChanged(object sender, EventArgs e)
        {
            label2.Text = $"The binded key is : {TxBxKTB.Text.ToString()}";
            TextBox objTextBox = (TextBox)sender;
            string text = objTextBox.Text;
            var TextBoxText = Enum.Parse(typeof(Keys), text);
        }

        //void setforground()
        //{
        //    IntPtr Calculator = FindWindow("Windows.UI.Core.CoreWindow", "Calculator");
        //    SetForegroundWindow(Calculator);
        //}

        bool isRunning = true;

        void Keyboard()
        {
            while (isRunning)
            {
                Thread.Sleep(40);
                if (Keyboard.IsKeyDown(Key.I want to set the users input key here))
                {
                    label1.Text = "pressed";
                    Thread.Sleep(40);
                    SendKeys.SendWait("e");
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            isRunning = false;
        }
    }
}
1 Answers

UPDATE

You could use Enum.Parse method to convert string to Key enum. It's safer to use TryParse to handle situations when string can't be converted to Key.

void Z()
{
    string key_string = "F10";
    if (Enum.TryParse(key_string, out Key key))
    {
        if (Keyboard.IsKeyDown(key))
        {
            // some logic
        }
    }
    else
    {
        // ERROR: the string couldn't be converted to Key
    }
}
Related