Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 16-03-2009
Member
 
Join Date: May 2008
Posts: 859
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 = "-";
} 
}
Reply With Quote
  #2  
Old 16-03-2009
Member
 
Join Date: May 2008
Posts: 3,971
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?
Reply With Quote
  #3  
Old 16-03-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Actions depending on the length of the press of a button

yes to treat the two events.
Keydown and KeyUp
Reply With Quote
  #4  
Old 16-03-2009
Member
 
Join Date: May 2008
Posts: 859
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; 
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Actions depending on the length of the press of a button"
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


All times are GMT +5.5. The time now is 03:49 PM.