Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

C# how can I do something if I press a key only one time

while true
{
 if (checkBox1.Checked & KKEY == "CapsLock")
  {
    if (sim.InputDeviceState.IsKeyDown(VirtualKeyCode.CAPITAL))
       {
        (Send something)
        Thread.Sleep(100);
       }
   }
 }

I want to know how can I send something only one time in this code.

I tried to delay the sends with thread.sleep because when I press the key it sends multiple times but I want only to send something 1 time per key click.

I tried to break but that cut the if and it only send the key one time and then never send it again.

Is there any way to do what I want to do? Thanks!

Edit: I forgot to put the while sorry. Holding the key should be one click and when is pressed again i need the send something again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I am understanding your question correctly, a solution somewhat like this should work:

bool keyPressed = false;
if (checkBox1.Checked & KKEY == "CapsLock")
{
    if (sim.InputDeviceState.IsKeyDown(VirtualKeyCode.CAPITAL) && !keyPressed)
    {
        keyPressed = true;
        (Send something)
                    
    }
    if (sim.InputDeviceState.IsKeyUp(VirtualKeyCode.CAPITAL))
    {
        keyPressed = false;            
    }
}

I am not that familiar with "sim" but you can modify the code as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...