how to simulate mouse click in win7 with direct port access
Posted: Tue Aug 26, 2014 8:14 pm
Hi,
i did search the articles in this forum, but not found the answer.
I'm using windows7 64bit system with an optical mouse with left button, right button and a wheel. I'm using winring0 driver for the direct hardware port access. My binary project is written in c# and only built for x86 (32bit binary).
I've verified to simulate the keyboard input in my project with following code:
uint status = (uint)ReadIoPortByte(0x64);
while ((status & 0x03) > 0)
{
Thread.Sleep(20);
status = (uint)ReadIoPortByte(0x64);
}
WriteIoPortByte(0x64, 0xd2);
Thread.Sleep(1); //if no sleep is called here, it does not work
WriteIoPortByte(0x60, scanCode);
uint status = (uint)ReadIoPortByte(0x64);
while ((status & 0x03) > 0)
{
Thread.Sleep(20);
status = (uint)ReadIoPortByte(0x64);
}
WriteIoPortByte(0x64, 0xd2);
Thread.Sleep(1);
WriteIoPortByte(0x60, (Byte)((uint)scanCode | 0x80));
But it does not work to simulate the mouse left click with the following key code:
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x09);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x08);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
I also inserted some sleep(n) functions, check the bit0 and/or bit1 of status register read from 0x64, check the ACK(0xfa) read from 0x60, send extra/less commands with byte 0x00 to mouse, tried all the possible combinations. I did see it once works, very stable, but, the next day, after reboot the system, it no longer works. I'm unable to reproduce the good case any more these days.
another thing need to mention is: i tried the code in win7 32bit system in VirtualBox on the same machine, it works, very stable. I have not verify if it relative to 32bit VS 64bit OS.
thanks a lot
i did search the articles in this forum, but not found the answer.
I'm using windows7 64bit system with an optical mouse with left button, right button and a wheel. I'm using winring0 driver for the direct hardware port access. My binary project is written in c# and only built for x86 (32bit binary).
I've verified to simulate the keyboard input in my project with following code:
uint status = (uint)ReadIoPortByte(0x64);
while ((status & 0x03) > 0)
{
Thread.Sleep(20);
status = (uint)ReadIoPortByte(0x64);
}
WriteIoPortByte(0x64, 0xd2);
Thread.Sleep(1); //if no sleep is called here, it does not work
WriteIoPortByte(0x60, scanCode);
uint status = (uint)ReadIoPortByte(0x64);
while ((status & 0x03) > 0)
{
Thread.Sleep(20);
status = (uint)ReadIoPortByte(0x64);
}
WriteIoPortByte(0x64, 0xd2);
Thread.Sleep(1);
WriteIoPortByte(0x60, (Byte)((uint)scanCode | 0x80));
But it does not work to simulate the mouse left click with the following key code:
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x09);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x08);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
WriteIoPortByte(0x64, 0xd3);
WriteIoPortByte(0x60, 0x00);
I also inserted some sleep(n) functions, check the bit0 and/or bit1 of status register read from 0x64, check the ACK(0xfa) read from 0x60, send extra/less commands with byte 0x00 to mouse, tried all the possible combinations. I did see it once works, very stable, but, the next day, after reboot the system, it no longer works. I'm unable to reproduce the good case any more these days.
another thing need to mention is: i tried the code in win7 32bit system in VirtualBox on the same machine, it works, very stable. I have not verify if it relative to 32bit VS 64bit OS.
thanks a lot