473,289 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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
Nov 13 '05 #1
4 32889
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]
- ni**************@exisconsulting.com

"Ralf Toender" <ra**@toender.de> wrote in message
news:04****************************@phx.gbl...
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

Nov 13 '05 #2
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" <ra**@toender.de> wrote in message
news:04****************************@phx.gbl...
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

Nov 13 '05 #3
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
-----Original Message-----
Hi Ralf,

I have tested a demo that I can send the WM_CHAR message from one form tothe textBox on the other form, even if the other form is minimized orhidden.

//delcare the SendMessage API
public class win32
{
[DllImport("USER32.DLL")]
public static extern int SendMessage (IntPtr hwnd,int msg,intcharacter,int count);
}
//Use the following code to call the API to send the message win32.SendMessage (mf.textBox1.Handle,0x0102,65,1); //0x0102 is the WM_CHARmessage

[NOTE]
mf is a Form object and textBox1 is a TextBox control.

Best regards
Peter Huang
--------------------
Content-Class: urn:content-classes:message
From: "Ralf Toender" <ra**@toender.de>
Sender: "Ralf Toender" <ra**@toender.de>
References: <04****************************@phx.gbl>

<O5**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: Keyboard input
Date: Wed, 9 Jul 2003 06:21:10 -0700
Lines: 44
Message-ID: <06****************************@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: AcNGHPaQFDpqnapeS+mTyngsQmxdxw==
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl! cpmsftngxa09.phx.gblXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:167925X-Tomcat-NG: microsoft.public.dotnet.languages.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 foundthe 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
-----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]
- ni**************@exisconsulting.com

"Ralf Toender" <ra**@toender.de> wrote in message
news:04****************************@phx.gbl.. .
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
.


.

Nov 13 '05 #4

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("USER32.DLL")]
public static extern int SendMessage(IntPtr hwnd,int
msg,int character,int count);
[DllImport("USER32.DLL")]
public static extern int PostMessage(IntPtr hwnd,int
msg,int character,int count);
[DllImport("USER32.DLL")]
public static extern int SendNotifyMessage(IntPtr hwnd,int
msg,int character,int count);
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(
string lpszClass, // class name
string lpszWindow // window name
);
[DllImport("USER32.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.FindWindow("WindowsForms10.Window.8.app3","F orm1");//"button1");
IntPtr hwnd = win32.FindWindowEx(phwnd,IntPtr.Zero
,"WindowsForms10.BUTTON.app3","button1");//"button1");
win32.SendMessage(hwnd,0x0100,0x20,0);//0x0100 is KeyDown,0x20 is
Space key
win32.SendMessage(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.

--------------------
Content-Class: urn:content-classes:message
From: "Ralf Toender" <ra**@toender.de>
Sender: "Ralf Toender" <ra**@toender.de>
References: <04****************************@phx.gbl> <O5**************@TK2MSFTNGP10.phx.gbl>
<06****************************@phx.gbl>
<8o**************@cpmsftngxa06.phx.gbl>Subject: Re: Keyboard input
Date: Thu, 10 Jul 2003 15:36:44 -0700
Lines: 112
Message-ID: <03****************************@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: AcNHM71Px5vJCP92SZezmHjMhbfXSQ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:168394
NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.languages.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
-----Original Message-----
Hi Ralf,

I have tested a demo that I can send the WM_CHAR message

from one form to
the textBox on the other form, even if the other form is

minimized or
hidden.

//delcare the SendMessage API
public class win32
{
[DllImport("USER32.DLL")]
public static extern int SendMessage

(IntPtr hwnd,int msg,int
character,int count);
}
//Use the following code to call the API to send

the message
win32.SendMessage

(mf.textBox1.Handle,0x0102,65,1); //0x0102 is the WM_CHAR
message

[NOTE]
mf is a Form object and textBox1 is a TextBox control.

Best regards
Peter Huang
--------------------
Content-Class: urn:content-classes:message
From: "Ralf Toender" <ra**@toender.de>
Sender: "Ralf Toender" <ra**@toender.de>
References: <04****************************@phx.gbl>

<O5**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: Keyboard input
Date: Wed, 9 Jul 2003 06:21:10 -0700
Lines: 44
Message-ID: <06****************************@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: AcNGHPaQFDpqnapeS+mTyngsQmxdxw==
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!cpmsftngxa09.phx.gblXref: cpmsftngxa06.phx.gblmicrosoft.public.dotnet.languages.csharp:167925X-Tomcat-NG: microsoft.public.dotnet.languages.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 Ifoundthe 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

-----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]
- ni**************@exisconsulting.com

"Ralf Toender" <ra**@toender.de> wrote in message
news:04****************************@phx.gbl. ..
> 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
.


.


Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ray | last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I enter data into fields of tables, queries and forms of Access 2003, it automatically switches to Chinese keyboard input. ...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
4
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard,...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
1
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the...
3
by: NaN | last post by:
I've been trying to use _kbhit() but it didn't do what I thought it would from books, "Detects whether a keypress is available for reading." Herbert Schildt says, "If the user has pressed a key,...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.