Use winio to simulate keyboard and mouse in C #

Source: Internet
Author: User

Sorry, I forgot the original address.


Public class winiolab
{
Private const int kbc_key_cmd = 0x64;
Private const int kbc_key_data = 0x60;
[Dllimport ("winio32.dll")]
Private Static extern bool initializewinio ();
[Dllimport ("winio32.dll")]
Private Static extern bool getportval (intptr wportaddr, out int pdwportval, byte bsize );
[Dllimport ("winio32.dll")]
Private Static extern bool setportval (uint wportaddr, intptr dwportval, byte bsize );
[Dllimport ("winio32.dll")]
Private Static extern byte mapphystolin (byte pbphysaddr, uint dwphyssize, intptr physicalmemoryhandle );
[Dllimport ("winio32.dll")]
Private Static extern bool unmapphysicalmemory (intptr physicalmemoryhandle, byte pblinaddr );
[Dllimport ("winio32.dll")]
Private Static extern bool getphyslong (intptr pbphysaddr, byte pdwphysval );
[Dllimport ("winio32.dll")]
Private Static extern bool setphyslong (intptr pbphysaddr, byte dwphysval );
[Dllimport ("winio32.dll")]
Private Static extern void shutdownwinio ();
[Dllimport ("user32.dll")]
Private Static extern int mapvirtualkey (uint ucode, uint umaptype );

Private winiolab ()
{
Isinitialize = true;
}
Public static void initialize ()
{
If (initializewinio ())
{
Kbcwait4ibe ();
Isinitialize = true;
}
}
Public static void Shutdown ()
{
If (isinitialize)
Shutdownwinio ();
Isinitialize = false;
}

Private Static bool isinitialize {Get; set ;}

/// Wait for the keyboard buffer to be empty
Private Static void kbcwait4ibe ()
{
Int dwval = 0;
Do
{
Bool flag = getportval (intptr) 0x64, out dwval, 1 );
}
While (dwval & 0x2)> 0 );
}
/// Press the analog keyboard
Public static void keydown (Keys vkeycoad)
{
If (! Isinitialize) return;

Int btscancode = 0;
Btscancode = mapvirtualkey (uint) vkeycoad, 0 );
Kbcwait4ibe ();
Setportval (kbc_key_cmd, (intptr) 0xd2, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_data, (intptr) 0x60, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_cmd, (intptr) 0xd2, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_data, (intptr) btscancode, 1 );
}
/// Simulate keyboard pop-up
Public static void keyup (Keys vkeycoad)
{
If (! Isinitialize) return;

Int btscancode = 0;
Btscancode = mapvirtualkey (uint) vkeycoad, 0 );
Kbcwait4ibe ();
Setportval (kbc_key_cmd, (intptr) 0xd2, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_data, (intptr) 0x60, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_cmd, (intptr) 0xd2, 1 );
Kbcwait4ibe ();
Setportval (kbc_key_data, (intptr) (btscancode | 0x80), 1 );
}
}

 

Call


Winiolab. initialize (); // register
Winiolab. keydown (Keys. a); // press
Winiolab. keyup (Keys. a); // release
Winiolab. Shutdown (); // log out after use


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.