473,757 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 32946
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************* *@exisconsultin g.com

"Ralf Toender" <ra**@toender.d e> 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.d e> 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("USER 32.DLL")]
public static extern int SendMessage (IntPtr hwnd,int msg,intcharacter,in t count);
}
//Use the following code to call the API to send the message win32.SendMessa ge (mf.textBox1.Ha ndle,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.d e>
Sender: "Ralf Toender" <ra**@toender.d e>
References: <04************ *************** *@phx.gbl>

<O5*********** ***@TK2MSFTNGP1 0.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: 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! cpmsftngxa09.ph x.gblXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1679 25X-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 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************* *@exisconsultin g.com

"Ralf Toender" <ra**@toender.d e> wrote in message
news:04***** *************** ********@phx.gb l...
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("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.

--------------------
Content-Class: urn:content-classes:message
From: "Ralf Toender" <ra**@toender.d e>
Sender: "Ralf Toender" <ra**@toender.d e>
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: 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
-----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("USER 32.DLL")]
public static extern int SendMessage

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

the message
win32.SendMessa ge

(mf.textBox1.H andle,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.d e>
Sender: "Ralf Toender" <ra**@toender.d e>
References : <04************ *************** *@phx.gbl>

<O5********** ****@TK2MSFTNGP 10.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: 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!cpmsftngxa09.p hx.gblXref: cpmsftngxa06.ph x.gblmicrosoft.publ ic.dotnet.langu ages.csharp:167 925X-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 Ifoundthe 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]
- ni************* *@exisconsultin g.com

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


.


Nov 13 '05 #5

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

Similar topics

0
2008
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. Even though I remove the Chinese keyboard input from Regional applet, the system will automatically reinstall Chinese keyboard input when I enter data in Office 2003. After I switch from Chinese keyboard input back to English keyboard input, it...
23
7748
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 buffer ? are they same ? thanks ^^
4
3875
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, which should prompt next one, how can I do in C program?. Thanks. enter IP address:
7
10687
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 were a keyboard. I need to be able to identify input from the scanner and keyboard independently. I've looked at DirectX.DirectInput, and using user32.dll to hook into the keyboard messages, but neither method seems to allow for identification of...
0
6732
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 my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles. however, everytime I use the readfile function I am getting "object reference not set to an instant...
2
8769
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 my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles. however, everytime I use the readfile function I am getting "object reference not set to an instant...
7
26541
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 nCode, int wParam, int lParam) {
1
6824
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 message queue, regardless of what application has focus. I can identify the keystrokes and input device by registering for raw input (RegisterRawInputDevices) and processing the WM_INPUT message. This gives me the keystrokes and the ability to...
3
3004
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, this function returns true(non-0), but does not read the character. If no keystroke is pending, kbhit() returns false (0)." Here is the test code,
8
5301
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 has the focus and handle ctrl-c, ctrl-v, or any other shortcuts. I have the same shortcuts working in my app, but have not determined how to find out which control has focus. Would I set up a loop or code for each control at form level. Any help...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.