View Single Post
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