Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 21st, 2008, 9:44 PM   #11
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Left Mouse Click

Here is a simple console application I whipped up to demonstrate:
C# Syntax (Toggle Plain Text)
  1. using System.Runtime.InteropServices;
  2. using System.Windows.Forms;
  3.  
  4. namespace YourNamespaceHere
  5. {
  6. class Program
  7. {
  8. [DllImport("user32.dll")]
  9. static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
  10.  
  11. static void Main(string[] args)
  12. {
  13. keybd_event((byte)Keys.H, 0, 0, 0);
  14. keybd_event((byte)Keys.H, 0, 0x7F, 0);
  15. keybd_event((byte)Keys.E, 0, 0, 0);
  16. keybd_event((byte)Keys.E, 0, 0x7F, 0);
  17. keybd_event((byte)Keys.L, 0, 0, 0);
  18. keybd_event((byte)Keys.L, 0, 0x7F, 0);
  19. keybd_event((byte)Keys.L, 0, 0, 0);
  20. keybd_event((byte)Keys.L, 0, 0x7F, 0);
  21. keybd_event((byte)Keys.O, 0, 0, 0);
  22. keybd_event((byte)Keys.O, 0, 0x7F, 0);
  23. }
  24. }
  25. }
I did two things here: first, I used the System.Windows.Forms.Keys enumeration; this is basically the .NET version of the virtual-key constants. You need to cast them to byte first, and (if you're creating a console app, rather than a Windows forms app) add a reference in your project. To add a reference, right-click the project name in the Solution Explorer, say 'add reference', and on the .NET tab, there will be one for System.Windows.Forms. You need to do this before the matching using directive will work. If you're creating a forms app, the reference will already be there, and the using directive will too for any forms-derived classes you create.

The second thing I did was to use the constant 0x7F. This is simply the value of the KEYEVENTF_KEYUP constant. Remember that each 'key down' event needs to be matched with a corresponding 'key up' event, or the system will think a key is still being held down.

Anyways, if you create a new console application, and copy-paste my code above in, it should work fine (after you add the reference, of course). To run it, don't do it from the IDE. Rather, open up a command window (Start -> Run -> type 'cmd' and hit ENTER), navigate to the directory where your program executable is, and run it. You should get output similar to the following:
C:\PathToYourExecutable>NameOfYourExecutable

C:\PathToYourExecutable>hello
The red text is what you type at the prompt. Hit ENTER, and the prompt will re-appear with the word 'hello' typed in already (green text). This is because the program simulated the keystrokes, and they were still in the buffer.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Mar 21st, 2008, 10:57 PM   #12
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Re: Left Mouse Click

again, thankyou, that worked perfectly. also thank you for taking the time to explain what was going on.
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 22nd, 2008, 5:20 AM   #13
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Left Mouse Click

Quote:
Originally Posted by crawforddavid2006
again, thankyou, that worked perfectly. also thank you for taking the time to explain what was going on.
Glad to see it's working. However, I have a suggestion for you. Assuming you're creating a class to simulate input (and if you are, you can combine the mouse and keyboard simulation in the same class), you might want to 'wrap' the call to keybd_event() and have your public method use the Keys enumeration directly:
C# Syntax (Toggle Plain Text)
  1. public static class VirtualKeyboard
  2. {
  3. [DllImport("user32.dll")] static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
  4.  
  5. public static KeyDown(System.Windows.Forms.Keys key)
  6. {
  7. keybd_event((byte)key, 0, 0, 0);
  8. }
  9.  
  10.  
  11. public static KeyUp(System.Windows.Forms.Keys key)
  12. {
  13. keybd_event((byte)key, 0, 0x7F, 0);
  14. }
  15. }
Doing it like this has three advantages. First, you don't need to port in the virtual key constants; you can use the ones in the System.Windows.Forms.Keys enumeration. Second, by using an enum, the calling code can only simulate keys for which there is a defined value (no passing in random numeric values). Third, it's clearer because there are two methods that take a single parameter, rather than one method with four (arguably more obscure) parameters. I mean, knowing the names and parameters of those two methods there makes it pretty clear what they do.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementation of Mouse Events. smita Existing Project Development 3 Mar 15th, 2007 4:11 PM
Java applet mouse event. Some trouble... glopal Java 5 Mar 9th, 2007 4:59 PM
Hyperactive mouse Polyphemus_ Coder's Corner Lounge 4 Jan 13th, 2007 11:59 PM
Mouse problems coldDeath Coder's Corner Lounge 8 Jul 5th, 2006 6:11 PM
Help: Changing mouse input to keyboard? Alighieri C++ 2 Mar 17th, 2005 10:59 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC