locked
How to simulate a key press in C# RRS feed

  • Question

  • Hello,

    I am using C# 2005.  I have a form that when it performs a certain event I would like to "press" the "Enter" key for the user so they do not have to.

    Is there a way I can programatically press the "Enter" key for the user? 

    Thanks,

    Flea

    Sunday, March 18, 2007 10:14 PM

Answers

  •  TilakGopi wrote:

    Hi,

     If isimulating key strokes is inevitable ,  

    SendKeys.Send({"ENTER") or SendKeys.SendWait("ENTER")

     

    Thanx,

    Ch.T.Gopi Kumar.



    It has to be

    SendKeys.Send("{ENTER}") or SendKeys.Send("~") to get the enter key.  "ENTER" would send the letters E, N, T, E, R.
    theTroll
    Monday, March 19, 2007 2:00 PM

All replies

  • It is possible, I don't know if this help but ok

    in the event 'keydown' you can enter this:

    private void URL_Combobox_KeyDown(object sender, KeyEventArgs e)

    {

    if (e.KeyCode == Keys.Enter)
       {
        // some code what you wanna do
       }

    }

    Sunday, March 18, 2007 11:25 PM
  • Sending keystrokes is not usually a good idea because you have to make sure the keystroke gets to the right window. What is usually recommended is to call the event of the object that usually handles the enter key being pressed.

    For example, if you have a button that responds to the enter key and you want to call its click event, you can call the button's PerformClick() method (inherited from Control). Or, you can just set up a specific function to call that does what the enter key is supposed to do.

    RayW

    Sunday, March 18, 2007 11:36 PM
  • Hi,

     If isimulating key strokes is inevitable ,  

    SendKeys.Send("{ENTER}") or SendKeys.SendWait("{ENTER}")

     

    Thanx,

    Ch.T.Gopi Kumar.

    Monday, March 19, 2007 11:16 AM
  •  TilakGopi wrote:

    Hi,

     If isimulating key strokes is inevitable ,  

    SendKeys.Send({"ENTER") or SendKeys.SendWait("ENTER")

     

    Thanx,

    Ch.T.Gopi Kumar.



    It has to be

    SendKeys.Send("{ENTER}") or SendKeys.Send("~") to get the enter key.  "ENTER" would send the letters E, N, T, E, R.
    theTroll
    Monday, March 19, 2007 2:00 PM
  • Hi,

    Good catch, i updated my post.Thanx for pointing out.

    Regards,

    Ch.T.Gopi Kumar.

    Monday, March 19, 2007 2:19 PM
  • i want to use the sendKey feature in Windows.Window class.... i am not getting this feature in intellisense.. and moreover i cannot even add the namespace Windows.Forms to it ..

     

    suggest me how can implement it in this scenario..

    Friday, December 10, 2010 5:32 AM
  • I know it's been 2 years, but I thought this might help anyone with the same issue as Csharp_Help above.

    System.Windows.Forms.SendKeys.Send("{ENTER}");
    Thursday, May 31, 2012 8:22 AM
  • omg!

    was NOT expecting this to be that easy. too easy. easy at a level where it makes sense...


    Thursday, October 18, 2012 3:36 AM
  •         private void txtCost_KeyUp(object sender, KeyEventArgs e)
            {
                //MessageBox.Show(Convert.ToString(e.KeyValue));  //use Messagebox for check value of key
                   if (e.KeyValue == 13)
                    {
                          // this command.
                    }
            }
    Tuesday, April 2, 2013 2:42 PM