How to simulate the mouse and keyboard of all games:

Source: Internet
Author: User

How to simulate the mouse and keyboard of all games:

Driver-level simulation:

Driver-level simulation: read and write the hardware port of the keyboard directly!

 

Some game programs that use the DirectX interface bypass the Windows message mechanism when reading keyboard operations, while using directinput. This is because some games
High requirements for sex control, such as racing games, require quick response to keyboard input. Because Windows messages are in the form of queues, there will be a lot of delay in the transmission of messages, and sometimes it will be transmitted in one second.
Dozens of messages, which cannot meet the game's requirements. Directinput bypasses Windows messages and directly deals with keyboard drivers, which of course improves the efficiency. Therefore
As a result, no response is made to such a program whether using postmessage or keybd_event, because these functions are at a higher level. For such a program, you have to use a direct read/write key.
The disk port method is used to simulate hardware events. To use this method to simulate the keyboard, You need to first understand the knowledge of keyboard programming.


In the DOS era, when a user presses or releases a key, a keyboard interruption occurs (if the keyboard interruption is allowed ), in this way, the program will jump to the keyboard interrupt handler in the BIOS for execution. Open
Windows Device Manager, you can see that the keyboard controller is controlled by two ports. & H60 is the data port, which can read the keyboard data while & h64 is the control port,
Used to send control signals. That is, the key information of this keyboard can be read from Port & h60. When a byte is read from this port, the low 7 bits of this byte are the scan code of the key, while a table with 1-bit higher
Indicates whether to press or release the key. When the key is pressed, the highest bit is 0, which is called a code. when the key is released, the highest bit is 1, which is called a code disconnection. Since data can be read from this port to obtain the key information, write to this port
Input data to simulate the button! A friend who has used qbasic4.5 may know that QB has an out command that can write data to a specified port, while the Gini function can read data from the specified port. That
Let's first look at how to write code with QB:

If you want to simulate pressing a key and the scan code for this key is & h50, then

Out & h64, & hd2' sends data & hd2 to the & h64 port. This is a KBC command to write data to the keyboard.

Out & h60, & h50' sends the scan code & h50 to Port & h60, which indicates simulating the press of this key with the scan code & h50

What about releasing this key? As shown in the following code, the code for sending the key is:

Out & h64, & hd2' sends data & hd2 to the & h64 port. This is a KBC command to write data to the keyboard.

Out & h60, (& h50 or & h80) 'performs or operations on the scan code & h50 and Data & h80. You can locate 1 in the upper part of the scan code to get a broken code, indicating that the key is released.

 

Now the question is how to write data to the port in VB. In Windows, normal applications do not have the right to operate ports, so we need a driver to help us implement
Now. Here we can use a component winio to perform read/write port operations. What is winio? Winio is a free, non-registration, and source program-included
Windows2000 port operating driver components (available to http://www.internals.com/

Upper
Download ). It not only supports port operations, but also memory operations. It can be used not only in VB, but also in Delphi, Vc, and other environments, with excellent performance. Download this component. After decompression, you can
To see Several folders, the three files in the release folder are what we need, and the three files are winio. sys (used for win
Driver Under XP), winio. VxD (for win
Driver Under 98), winio. dll (encapsulation function Dynamic Link Library), we only need to call the function in winio. dll, then winio. dll will install and call the drive
To complete the corresponding functions. It is worth mentioning that this component is completely green without installation. You only need to copy the three files to the same folder as your program. Easy to use,
First install the driver using the initializewinio function, and then you can use getportval to read the port or use setportval to write the port. Good,
Let's make a driver-level keyboard simulation. Copy the three winio files to the folder of your program, create a new project in VB, add a module, and add the following
Winio function declaration:

Declare function mapphystolin lib "winio. dll" (byval physaddr as long, byval physsize as long, byref physmemhandle) as long

Declare function unmapphysicalmemory lib "winio. dll" (byval physmemhandle, byval linaddr) as Boolean

Declare function getphyslong lib "winio. dll" (byval physaddr as long, byref physval as long) as Boolean

Declare function setphyslong lib "winio. dll" (byval physaddr as long, byval physval as long) as Boolean

Declare function getportval lib "winio. dll" (byval portaddr as integer, byref portval as long, byval bsize as byte) as Boolean

Declare function setportval lib "winio. dll" (byval portaddr as integer, byval portval as long, byval bsize as byte) as Boolean

Declare function initializewinio lib "winio. dll" () as Boolean

Declare function shutdownwinio lib "winio. dll" () as Boolean

Declare function installwiniodriver lib "winio. dll" (byval driverpath as string, byval mode as integer) as Boolean

Declare function removewiniodriver lib "winio. dll" () as Boolean

'------------------------------------ The above is the winio function declaration -------------------------------------------

Declare function mapvirtualkey lib "USER32" alias "mapvirtualkeya" (byval wcode as long, byval wmaptype as long) as long

'----------------------------------- The preceding is the Win32 API function declaration -----------------------------------------

Add the following process:

Sub kbcwait4ibe () 'Wait for the keyboard buffer to be empty

Dim dwval as long

Do

Getportval & h64, dwval, 1

'Indicates reading a byte from Port & h64 and putting the read data into the variable dwval.

'Getportval is used as the getportval port number to store the read data variable and the read length.

Loop while (dwval and & H2)

End sub

The above is a write process according to the kbc specification. Its function is to wait for a period of time before writing data to the keyboard port, which will be used later.

Then add the following process to simulate the buttons:

Public const kbc_key_cmd = & h64' Keyboard Command Port

Public const kbc_key_data = & h60' keyboard data port

Sub mykeydown (byval vkeycoad as long)

'The simulated press Key, vkeycoad parameter passed in the virtual code of the key

Dim btscancode as long

Btscancode = mapvirtualkey (vkeycoad, 0)

Before sending data, kbcwait4ibe should wait for the keyboard buffer to be empty.

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

The 'setportval function is used to write data to the port. Its usage is the setportval port number, the data to be written, and the length of the data to be written.

Kbcwait4ibe

Setportval kbc_key_data, btscancode, 1 'write key information, press the key

End sub

Sub mykeyup (byval vkeycoad as long)

'This is used to simulate the release key. The virtual Code passed in by the vkeycoad parameter.

Dim btscancode as long

Btscancode = mapvirtualkey (vkeycoad, 0)

Kbcwait4ibe 'Wait for the keyboard buffer to be empty

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

Kbcwait4ibe

Setportval kbc_key_data, (btscancode or & h80), 1' write key information, release key

End sub

After defining the above process, you can use it to simulate keyboard input. Add a timer control to the form module and add the following code: private sub form_load () If initializewinio = false then

'Use the initializewinio function to load the driver. If it succeeds, true is returned. Otherwise, false is returned.

Msgbox "Driver loading failed! "

Unload me

End if

Timer1.integer = 3000

Timer1.enabled = true

End sub

Private sub form_unload (cancel as integer)

When the shutdownwinio program ends, remember to use the shutdownwinio function to uninstall the driver.

End sub

Private sub timer1_timer ()

Dim vk_a as long = & H41

Mykeydown vk_a

Mykeyup vk_a' simulate pressing and releasing key

End sub

[/Quote]

When running the above program, it will simulate pressing the key every three seconds. Try again. How is it? Is it effective for all programs?

Notes:

To use winio in VB debugging mode, copy the three files to the installation directory of VB.

Some keys on the keyboard are extension keys (for example, the arrow keys on the keyboard are extension keys). Do not use the mykeydown and mykeyup processes to simulate the extension keys, you can use the following two procedures to accurately simulate the extension key:

Quote:

Sub mykeydownex (byval vkeycoad as long) 'simulate the extension key press. The parameter vkeycoad is the virtual code of the extension key.

Dim btscancode as long

Btscancode = mapvirtualkey (vkeycoad, 0)

Kbcwait4ibe 'Wait for the keyboard buffer to be empty

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

Kbcwait4ibe

Setportval kbc_key_data, & he0, 1 'Write extension key flag Information

Kbcwait4ibe 'Wait for the keyboard buffer to be empty

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

Kbcwait4ibe

Setportval kbc_key_data, btscancode, 1 'write key information, press the key

End sub

Sub mykeyupex (byval vkeycoad as long )'

Dim btscancode as long

Btscancode = mapvirtualkey (vkeycoad, 0)

Kbcwait4ibe 'Wait for the keyboard buffer to be empty

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

Kbcwait4ibe

Setportval kbc_key_data, & he0, 1 'Write extension key flag Information

Kbcwait4ibe 'Wait for the keyboard buffer to be empty

Setportval kbc_key_cmd, & hd2, 1 'send the keyboard write command

Kbcwait4ibe

Setportval kbc_key_data, (btscancode or & h80), 1' write key information, release key

End sub

Also
It should be noted that if you want to convert from the extended key to the normal key, the keydown event of the normal key should be sent twice. That is to say, if I want to simulate pressing an extension key first and then pressing a common key,
Then, the common key is pushed twice to the port. For example, I want to simulate an event where I press the left arrow key and then the Space key. Because the left arrow key is an extension key and the Space key is a common key, the process is as follows:
It should be like this:

[Quote] mykeydownex vk_left 'press the left arrow key

Sleep 200 'latency 200 milliseconds

Mykeyupex vk_left 'release the left direction key

Sleep 500

Mykeydown vk_space 'press the Space key. Be sure to send it twice

Mykeydown vk_space

Sleep 200

Mykeyup vk_space 'release Space key

I used winio to simulate the left mouse click in VB. The specific code is as follows:

Private sub XR ()

Dim result as Boolean

Result = setportval (Val ("& h64"), Val ("& hd3"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Sleep 100

Result = setportval (Val ("& h64"), Val ("& hf4"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& h09"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& H00"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& H00"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& h08"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& H00"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

Result = setportval (Val ("& h60"), Val ("& H00"), 1)

If (result = false) then

Msgbox "whoops! There is a problem with setportbyte. ", vbokonly + vbcritical," vbdumpport32"

Unload frmvbdumpport32

End if

End sub

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.