Keep Key Pressed

J

Jean

Hello, I´m a new user to C# and I have a question.
How to make my application emulate a key pressed? Not just the key press,
but keep the key pressed?
For example, let´s say that I want to emulate the 'A' key:
-I click in a button.
-Then, the program focus a field textbox on my Form and in this field, the
string AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA start appear after the
focus.
-When I click in another button, the 'A' key is released.

I try to use some methods like SendKeys, SendMessage and even a old windows
Api keybd_event, but don´t have success.
Anyone can help me?

tks!
 
K

kimiraikkonen

Hello, I´m a new user to C# and I have a question.
How to make my application emulate a key pressed? Not just the key press,
but keep the key pressed?
For example, let´s say that I want to emulate the 'A' key:
-I click in a button.
-Then, the program focus a field textbox on my Form and in this field, the
string AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA start appear after the
focus.
-When I click in another button, the 'A' key is released.

I try to use some methods like SendKeys, SendMessage and even a old windows
Api keybd_event, but don´t have success.
Anyone can help me?

tks!

First, focus on textbox using textBox1.Focus();
Then, you can try SendKeys with Timer control. Timer will iterate
SendKeys method's keypress within specified interval in that case.

....or just let Timer generate "A"s for you like:
(in timer's tick event):

textBox1.Text = textBox1.Text & "A"

HTH,

Onur Güzel
 
K

kimiraikkonen

First, focus on textbox using textBox1.Focus();
Then, you can try SendKeys with Timer control. Timer will iterate
SendKeys method's keypress within specified interval in that case.

Sorry, i need to update. That works as follows:

private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("{A}");
}
...or just let Timer generate "A"s for you like:
(in timer's tick event):
textBox1.Text = textBox1.Text & "A"

Sorry, correcting the syntax:
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Focus();
textBox1.Text = textBox1.Text + "A";
}


HTH,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top