locked
Press a Keyboard Key using C#

    Question

  • How can I make a button when pressed (onclick) give a key code like the Windows On-Screen Keyboard (when I pres the A button it types a)? Using C#
    Friday, June 25, 2010 9:15 AM

Answers

  • Thats a VB code how can I do the Same using C#?

    Reference and using Microsoft.VisualBasic

          int pid = Interaction.Shell("notepad"AppWinStyle.NormalFocus, false, -1);
          
    Interaction.AppActivate(pid);
          
    SendKeys.Send("From my application to notepad");

          
    Interaction.AppActivate("notepad");
          
    SendKeys.Send("From my application to notepad");

    • Marked as answer by Ziad Akiki Saturday, June 26, 2010 9:16 AM
    Saturday, June 26, 2010 8:52 AM
  • I want to send keys to Notepad or any other application (from my app but not to it)


    The standard send keys to an application:

        To open the application and then send keys:

        Dim Pid As Integer = Shell("notepad", AppWinStyle.NormalFocus)
        AppActivate(Pid)
        SendKeys.Send(
    "From my application to notepad")

        To send keys to an open application:

        AppActivate(
    "notepad")
        SendKeys.Send(
    "From my application to notepad")

    • Marked as answer by Ziad Akiki Saturday, June 26, 2010 9:18 AM
    Friday, June 25, 2010 7:26 PM

All replies

    • Edited by Carsten Koenig Friday, June 25, 2010 9:33 AM I guess the en-version is better suited than the german
    • Marked as answer by Ziad Akiki Friday, June 25, 2010 12:32 PM
    • Unmarked as answer by Ziad Akiki Friday, June 25, 2010 12:32 PM
    Friday, June 25, 2010 9:31 AM
  • You can use SendKey.Send Method for the same.

    SendKeys.Send("A")


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, June 25, 2010 9:37 AM
  • Welcome to the MSDN Forums.

    You may be interested in an already implemented by FoxholeWilly at http://www.codeproject.com/KB/miscctrl/touchscreenkeyboard.aspx (Touchscreen Keyboard UserControl).

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 25, 2010 9:48 AM
  • I dont get your question. You did a mouse click and want to have a key kode. Of what? Of mouse click? Is this case there was no key pressed, it was only a mouse click.

    Can you be a bit more specific, please?

    Friday, June 25, 2010 9:52 AM
  • Ok, if I want to sendkey to Notepad from my Application, how can I do that?
    Friday, June 25, 2010 11:24 AM
  • Instead of sending keystrokes to another app(like Notepad), you could directly set the text on an edit control. In your notepad example that's the simplest way to change the text in the notepad window.
    http://blog.voidnish.com
    Friday, June 25, 2010 11:52 AM
    Moderator
  • How can I send keystrokes to another app?
    Friday, June 25, 2010 12:50 PM
  • Instead of sending keystrokes to another app(like Notepad), you could directly set the text on an edit control. In your notepad example that's the simplest way to change the text in the notepad window.
    http://blog.voidnish.com


    Yes, I have an almost same issue solution in my blog at http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!304.entry?sa=865072108 (How To: Exporting DataGridView directly to notepad via Windows API) which I hope helps.

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 25, 2010 12:53 PM
  • How can I make a button when pressed (onclick) give a key code like the Windows On-Screen Keyboard (when I pres the A button it types a)? Using C#


    Ok, if I want to sendkey to Notepad from my Application, how can I do that?

    Which do you want to do?  These are completely different.  To send a key to notepad, you use AppActivate to activate notepad and then SendKeys to send the keys.  An on-screen keyboard can't take the focus from the currently active window.  You don't know where to send the keys.

    Friday, June 25, 2010 2:05 PM
  • I want to send keys to Notepad or any other application (from my app but not to it)
    Friday, June 25, 2010 6:42 PM
  • I want to send keys to Notepad or any other application (from my app but not to it)


    Please see the solution at http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c7c787d0-9426-4b18-b15e-d43821bbb0ea/ (Send Key and mouse input to a application).

    Hope this helps enough.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 25, 2010 6:58 PM
  • I want to send keys to Notepad or any other application (from my app but not to it)


    The standard send keys to an application:

        To open the application and then send keys:

        Dim Pid As Integer = Shell("notepad", AppWinStyle.NormalFocus)
        AppActivate(Pid)
        SendKeys.Send(
    "From my application to notepad")

        To send keys to an open application:

        AppActivate(
    "notepad")
        SendKeys.Send(
    "From my application to notepad")

    • Marked as answer by Ziad Akiki Saturday, June 26, 2010 9:18 AM
    Friday, June 25, 2010 7:26 PM
  • Thats a VB code how can I do the Same using C#?
    Saturday, June 26, 2010 8:16 AM
  • VB to C# converter: HERE!

    JohnWein`s C# code:

    int Pid = Interaction.Shell("notepad", AppWinStyle.NormalFocus);
    
    Interaction.AppActivate(Pid);
    SendKeys.Send("From my application to notepad");
    
    Interaction.AppActivate("notepad");
    SendKeys.Send("From my application to notepad");
    

     

    Saturday, June 26, 2010 8:49 AM
  • Thats a VB code how can I do the Same using C#?

    Reference and using Microsoft.VisualBasic

          int pid = Interaction.Shell("notepad"AppWinStyle.NormalFocus, false, -1);
          
    Interaction.AppActivate(pid);
          
    SendKeys.Send("From my application to notepad");

          
    Interaction.AppActivate("notepad");
          
    SendKeys.Send("From my application to notepad");

    • Marked as answer by Ziad Akiki Saturday, June 26, 2010 9:16 AM
    Saturday, June 26, 2010 8:52 AM
  • Thanks a Lot!!
    Saturday, June 26, 2010 9:17 AM