Results 1 to 4 of 4

Thread: Actions depending on the length of the press of a button

  1. #1
    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 = "-";
    } 
    }

  2. #2
    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?

  3. #3
    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

  4. #4
    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; 
    }

Similar Threads

  1. Asus EEE keyboard keys press wrong button (I)
    By emo10 in forum Hardware Peripherals
    Replies: 1
    Last Post: 29-03-2012, 05:55 AM
  2. Automating Button Press with Hardware
    By Blesseds in forum Hardware Peripherals
    Replies: 3
    Last Post: 31-12-2010, 01:13 PM
  3. Detect key or button press in SDL
    By RyanInt in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 07:01 PM
  4. Zune player freezes when i press any button
    By Author in forum Portable Devices
    Replies: 3
    Last Post: 07-10-2009, 09:50 PM
  5. Impossible to press a button to install windows xp
    By Jaden in forum Operating Systems
    Replies: 2
    Last Post: 28-11-2008, 03:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,616,404.52778 seconds with 17 queries