L
L
lens23142021-10-30 20:46:20
C++ / C#
lens2314, 2021-10-30 20:46:20

How to make a key binding?

What I want to convey
I am writing a key binding script, which will start the macro
How to make such a key binding and it is mandatory that in a Windows Form? the binding process should take place when the user clicks on the button1 button, that is, in the button1_Click method.

What I tried:
1. make a truncated similarity of a keylogger, but there was more information on console applications and on the C ++ language
2. this article from stackoverflow
but did not understand what stands for

private void MainForm_Load(object sender, EventArgs e)
    {
        hook.KeyDown += (s, ev) => {
            // Так или иначе свернём приложение и т.д.,
            // при необходимости можно проверить, какая именно кнопка была нажата
            
        };

Therefore, I could not apply the received answer for my own purposes.
3. use ReadKey, but the program gives an error
System.InvalidOperationException: "Unable to read keys when any application does not have a console or console input was redirected from a file. Try using Console.Read."

4.
private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)  
{  
    listBox1.Items.Add(e.KeyCode);  
    
}


I also asked a similar question on the same site, but the wording turned out to be incorrect. question

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-10-31
@BasiC2k

Google examples of global keyboard hook
It uses interception of keystrokes using the Win API. C++ is not needed.
When you understand how keystrokes are intercepted, you can adapt to your needs.
PS do not forget to return control after interception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question