|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Actions depending on the length of the press of a button I would like to code in C# such a way that when I press the spacebar more than "n" time I want a certain character and if it is less than "n" time I want to display a different character. Do someone have any ideas? I do it for some time but unfortunately nothing interesting! Thank you very much Code: private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { Stopwatch Temps = new Stopwatch(); if (e.KeyCode == Keys.Back) { Temps.Start(); } if (e.KeyData != Keys.Back) { Temps.Stop(); } TimeSpan ts = Temps.Elapsed; if (ts.Milliseconds > 1000) { textBox2.Text = "."; } else { textBox2.Text = "-"; } } |
#2
| |||
| |||
Re: Actions depending on the length of the press of a button Why not put your Stopwatch in the declaration of your form, activate it in the keydown, stop in the KeyUp, and then recover the value of the Stopwatch and update your TextBox? |
#3
| |||
| |||
Re: Actions depending on the length of the press of a button yes to treat the two events. Keydown and KeyUp |
#4
| |||
| |||
Re: Actions depending on the length of the press of a button So after extensive research, already I take F3 as key support! I have this: Code: private DateTime t1; private bool isKeyDown = false; private void textBox2_KeyDown (object sender, KeyEventArgs e) { if (!isKeyDown && e.KeyCode == Keys.F3) { isKeyDown = true; t1 = DateTime.Now; } } private void textBox2_KeyUp (object sender, KeyEventArgs e) { if (isKeyDown) { isKeyDown = false; WriteCode (sender, DateTime.Now - t1); } } private void WriteCode (object sender, TimeSpan tps) { String toWrite; if (tps.TotalMilliseconds >= 1000) toWrite = "-"; else toWrite = "." textBox2.Text = toWrite; } |
![]() |
|
Tags: actions, button, depending, length, press |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Asus EEE keyboard keys press wrong button (I) | emo10 | Hardware Peripherals | 1 | 29-03-2012 05:55 AM |
Automating Button Press with Hardware | Blesseds | Hardware Peripherals | 3 | 31-12-2010 01:13 PM |
Detect key or button press in SDL | RyanInt | Software Development | 5 | 22-01-2010 07:01 PM |
Zune player freezes when i press any button | Author | Portable Devices | 3 | 07-10-2009 09:50 PM |
Impossible to press a button to install windows xp | Jaden | Operating Systems | 2 | 28-11-2008 03:30 PM |