Keyboard input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralf Toender

    #1

    Keyboard input

    Hi!

    Does anybody know how to send a keystroke C#? In C++ it's
    done by: keybd_event ( ... ) or SendInput ( ... ).
    What namespace does include this counterpart?

    Thanks

    Ralf
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Keyboard input

    Ralf,

    Your best bet would be to use the SendKeys class to send keyboard
    strokes to your application.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Ralf Toender" <ralf@toender.d e> wrote in message
    news:043601c346 05$0c9b21e0$a00 1280a@phx.gbl.. .[color=blue]
    > Hi!
    >
    > Does anybody know how to send a keystroke C#? In C++ it's
    > done by: keybd_event ( ... ) or SendInput ( ... ).
    > What namespace does include this counterpart?
    >
    > Thanks
    >
    > Ralf[/color]


    Comment

    • Ignacio Machin

      #3
      Re: Keyboard input

      Hi Ralf,

      I have never use it, but I know there is a class named SendKeys in
      System.Windows. Forms , take a look at SendKeys.Send() , it may be the
      function you are looking for.

      Hope this help,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Ralf Toender" <ralf@toender.d e> wrote in message
      news:043601c346 05$0c9b21e0$a00 1280a@phx.gbl.. .[color=blue]
      > Hi!
      >
      > Does anybody know how to send a keystroke C#? In C++ it's
      > done by: keybd_event ( ... ) or SendInput ( ... ).
      > What namespace does include this counterpart?
      >
      > Thanks
      >
      > Ralf[/color]


      Comment

      • Ralf Toender

        #4
        Re: Keyboard input

        Hi Peter!

        Thanks a lot! What about 2 apps - one playing the keyboard
        and the other is the controlled one? For example one is a
        touchscreen controlling the other app?

        Cheers Ralf


        [color=blue]
        >-----Original Message-----
        >Hi Ralf,
        >
        >I have tested a demo that I can send the WM_CHAR message[/color]
        from one form to[color=blue]
        >the textBox on the other form, even if the other form is[/color]
        minimized or[color=blue]
        >hidden.
        >
        > //delcare the SendMessage API
        > public class win32
        > {
        > [DllImport("USER 32.DLL")]
        > public static extern int SendMessage[/color]
        (IntPtr hwnd,int msg,int[color=blue]
        >character,in t count);
        > }
        > //Use the following code to call the API to send[/color]
        the message[color=blue]
        > win32.SendMessa ge[/color]
        (mf.textBox1.Ha ndle,0x0102,65, 1); //0x0102 is the WM_CHAR[color=blue]
        >message
        >
        >[NOTE]
        >mf is a Form object and textBox1 is a TextBox control.
        >
        >Best regards
        >Peter Huang
        >--------------------[color=green]
        >>Content-Class: urn:content-classes:message
        >>From: "Ralf Toender" <ralf@toender.d e>
        >>Sender: "Ralf Toender" <ralf@toender.d e>
        >>References: <043601c34605$0 c9b21e0$a001280 a@phx.gbl>[/color]
        ><O52AjdhRDHA.2 460@TK2MSFTNGP1 0.phx.gbl>[color=green]
        >>Subject: Re: Keyboard input
        >>Date: Wed, 9 Jul 2003 06:21:10 -0700
        >>Lines: 44
        >>Message-ID: <064201c3461c$f 6900c90$a001280 a@phx.gbl>
        >>MIME-Version: 1.0
        >>Content-Type: text/plain;
        >> charset="iso-8859-1"
        >>Content-Transfer-Encoding: 7bit
        >>X-Newsreader: Microsoft CDO for Windows 2000
        >>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
        >>Thread-Index: AcNGHPaQFDpqnap eS+mTyngsQmxdxw ==
        >>Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        >>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
        >>Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl![/color][/color]
        cpmsftngxa09.ph x.gbl[color=blue][color=green]
        >>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        microsoft.publi c.dotnet.langua ges.csharp:1679 25[color=blue][color=green]
        >>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        >>
        >>Super! Thanks - that's it :-)
        >>
        >>One more question: to send the keystrokes to another
        >>application it has to get the focus. In one sample I[/color][/color]
        found[color=blue][color=green]
        >>the combination: Hide () ... Show () but this makes the
        >>form flicker. How can I give the focus back to the other
        >>application which is running and waiting for the strokes
        >>to come? Some kind of Deactivate () or something like
        >>SetFocus ( FindWindow ( ... ) );
        >>
        >>Thanks
        >>
        >>Ralf
        >>[color=darkred]
        >>>-----Original Message-----
        >>>Ralf,
        >>>
        >>> Your best bet would be to use the SendKeys class to[/color]
        >>send keyboard[color=darkred]
        >>>strokes to your application.
        >>>
        >>> Hope this helps.
        >>>
        >>>
        >>>--
        >>> - Nicholas Paldino [.NET/C# MVP]
        >>> - nicholas.paldin o@exisconsultin g.com
        >>>
        >>>"Ralf Toender" <ralf@toender.d e> wrote in message
        >>>news:043601c 34605$0c9b21e0$ a001280a@phx.gb l...
        >>>> Hi!
        >>>>
        >>>> Does anybody know how to send a keystroke C#? In C++[/color]
        >>it's[color=darkred]
        >>>> done by: keybd_event ( ... ) or SendInput ( ... ).
        >>>> What namespace does include this counterpart?
        >>>>
        >>>> Thanks
        >>>>
        >>>> Ralf
        >>>
        >>>
        >>>.
        >>>[/color]
        >>[/color]
        >
        >.
        >[/color]

        Comment

        • Peter Huang [MSFT]

          #5
          Re: Keyboard input


          Hi Ralf,

          It is not easy to control an application from another application because
          the other application may be very complicated. If you really want to do so,
          you may call the FindWindow and FindWindowEx APIs to get the handle of the
          button on the form in another application, then call the SendMessage API to
          send KeyDown and KeyUp Messages to the button. Here is some simple code.

          //here is the API declare
          public class win32
          {
          [DllImport("USER 32.DLL")]
          public static extern int SendMessage(Int Ptr hwnd,int
          msg,int character,int count);
          [DllImport("USER 32.DLL")]
          public static extern int PostMessage(Int Ptr hwnd,int
          msg,int character,int count);
          [DllImport("USER 32.DLL")]
          public static extern int SendNotifyMessa ge(IntPtr hwnd,int
          msg,int character,int count);
          [DllImport("USER 32.DLL")]
          public static extern IntPtr FindWindow(
          string lpszClass, // class name
          string lpszWindow // window name
          );
          [DllImport("USER 32.DLL")]
          public static extern IntPtr FindWindowEx(
          IntPtr hwndParent, // handle to parent window
          IntPtr hwndChildAfter, // handle to child window
          string lpszClass, // class name
          string lpszWindow // window name
          );
          }
          //here is about how to invoke the API function.
          //you may get the window class name via the spy++ include in Visual
          Studio .NET
          IntPtr phwnd =
          win32.FindWindo w("WindowsForms 10.Window.8.app 3","Form1");//"button1");
          IntPtr hwnd = win32.FindWindo wEx(phwnd,IntPt r.Zero
          ,"WindowsForms1 0.BUTTON.app3", "button1");//"button1");
          win32.SendMessa ge(hwnd,0x0100, 0x20,0);//0x0100 is KeyDown,0x20 is
          Space key
          win32.SendMessa ge(hwnd,0x0101, 0x20,1);//0x0101 is KeyUp



          Hope this will help you.

          Best regards
          Peter Huang

          =============
          This posting is provided "AS IS" with no warranties, and confers no rights.

          --------------------[color=blue]
          >Content-Class: urn:content-classes:message
          >From: "Ralf Toender" <ralf@toender.d e>
          >Sender: "Ralf Toender" <ralf@toender.d e>
          >References: <043601c34605$0 c9b21e0$a001280 a@phx.gbl>[/color]
          <O52AjdhRDHA.24 60@TK2MSFTNGP10 .phx.gbl>
          <064201c3461c$f 6900c90$a001280 a@phx.gbl>
          <8oQwKXuRDHA.16 36@cpmsftngxa06 .phx.gbl>[color=blue]
          >Subject: Re: Keyboard input
          >Date: Thu, 10 Jul 2003 15:36:44 -0700
          >Lines: 112
          >Message-ID: <033d01c34733$b d4fbe90$a401280 a@phx.gbl>
          >MIME-Version: 1.0
          >Content-Type: text/plain;
          > charset="iso-8859-1"
          >Content-Transfer-Encoding: 7bit
          >X-Newsreader: Microsoft CDO for Windows 2000
          >Thread-Index: AcNHM71Px5vJCP9 2SZezmHjMhbfXSQ ==
          >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
          >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
          >Path: cpmsftngxa06.ph x.gbl
          >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1683 94
          >NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
          >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
          >
          >Hi Peter!
          >
          >Thanks a lot! What about 2 apps - one playing the keyboard
          >and the other is the controlled one? For example one is a
          >touchscreen controlling the other app?
          >
          >Cheers Ralf
          >
          >
          >[color=green]
          >>-----Original Message-----
          >>Hi Ralf,
          >>
          >>I have tested a demo that I can send the WM_CHAR message[/color]
          >from one form to[color=green]
          >>the textBox on the other form, even if the other form is[/color]
          >minimized or[color=green]
          >>hidden.
          >>
          >> //delcare the SendMessage API
          >> public class win32
          >> {
          >> [DllImport("USER 32.DLL")]
          >> public static extern int SendMessage[/color]
          >(IntPtr hwnd,int msg,int[color=green]
          >>character,i nt count);
          >> }
          >> //Use the following code to call the API to send[/color]
          >the message[color=green]
          >> win32.SendMessa ge[/color]
          >(mf.textBox1.H andle,0x0102,65 ,1); //0x0102 is the WM_CHAR[color=green]
          >>message
          >>
          >>[NOTE]
          >>mf is a Form object and textBox1 is a TextBox control.
          >>
          >>Best regards
          >>Peter Huang
          >>--------------------[color=darkred]
          >>>Content-Class: urn:content-classes:message
          >>>From: "Ralf Toender" <ralf@toender.d e>
          >>>Sender: "Ralf Toender" <ralf@toender.d e>
          >>>References : <043601c34605$0 c9b21e0$a001280 a@phx.gbl>[/color]
          >><O52AjdhRDHA. 2460@TK2MSFTNGP 10.phx.gbl>[color=darkred]
          >>>Subject: Re: Keyboard input
          >>>Date: Wed, 9 Jul 2003 06:21:10 -0700
          >>>Lines: 44
          >>>Message-ID: <064201c3461c$f 6900c90$a001280 a@phx.gbl>
          >>>MIME-Version: 1.0
          >>>Content-Type: text/plain;
          >>> charset="iso-8859-1"
          >>>Content-Transfer-Encoding: 7bit
          >>>X-Newsreader: Microsoft CDO for Windows 2000
          >>>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
          >>>Thread-Index: AcNGHPaQFDpqnap eS+mTyngsQmxdxw ==
          >>>Newsgroups : microsoft.publi c.dotnet.langua ges.csharp
          >>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
          >>>Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl![/color][/color]
          >cpmsftngxa09.p hx.gbl[color=green][color=darkred]
          >>>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
          >microsoft.publ ic.dotnet.langu ages.csharp:167 925[color=green][color=darkred]
          >>>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
          >>>
          >>>Super! Thanks - that's it :-)
          >>>
          >>>One more question: to send the keystrokes to another
          >>>applicatio n it has to get the focus. In one sample I[/color][/color]
          >found[color=green][color=darkred]
          >>>the combination: Hide () ... Show () but this makes the
          >>>form flicker. How can I give the focus back to the other
          >>>applicatio n which is running and waiting for the strokes
          >>>to come? Some kind of Deactivate () or something like
          >>>SetFocus ( FindWindow ( ... ) );
          >>>
          >>>Thanks
          >>>
          >>>Ralf
          >>>
          >>>>-----Original Message-----
          >>>>Ralf,
          >>>>
          >>>> Your best bet would be to use the SendKeys class to
          >>>send keyboard
          >>>>strokes to your application.
          >>>>
          >>>> Hope this helps.
          >>>>
          >>>>
          >>>>--
          >>>> - Nicholas Paldino [.NET/C# MVP]
          >>>> - nicholas.paldin o@exisconsultin g.com
          >>>>
          >>>>"Ralf Toender" <ralf@toender.d e> wrote in message
          >>>>news:043601 c34605$0c9b21e0 $a001280a@phx.g bl...
          >>>>> Hi!
          >>>>>
          >>>>> Does anybody know how to send a keystroke C#? In C++
          >>>it's
          >>>>> done by: keybd_event ( ... ) or SendInput ( ... ).
          >>>>> What namespace does include this counterpart?
          >>>>>
          >>>>> Thanks
          >>>>>
          >>>>> Ralf
          >>>>
          >>>>
          >>>>.
          >>>>
          >>>[/color]
          >>
          >>.
          >>[/color]
          >[/color]

          Comment

          Working...