how to simulate mouse click in win7 with direct port access

Programming, for all ages and all languages.
Post Reply
guoguo
Posts: 5
Joined: Tue Aug 26, 2014 7:50 pm

how to simulate mouse click in win7 with direct port access

Post by guoguo »

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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: how to simulate mouse click in win7 with direct port acc

Post by Brendan »

Hi,

Some minor notes...

a) Most modern systems use USB keyboard and mouse, and therefore this code will fail in most cases.

b) This code has severe race conditions (e.g. nothing prevents the real driver screwing up your hack at the wrong time). With the approach you're using these severe race conditions are unsolvable.

c) The correct approach would be to put something between the real driver/s and the GUI, and not to slap a hideous hackfest between the real driver and the hardware. I'm fairly certain Windows has some sort of API/interface that's actually intended for this (e.g. for IME support).

d) This isn't a Windows development forum; it's a forum for people developing new/alternative OSs


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
guoguo
Posts: 5
Joined: Tue Aug 26, 2014 7:50 pm

Re: how to simulate mouse click in win7 with direct port acc

Post by guoguo »

thanks a lot Brendan.

a) yes, i'm using usb keyboard and usb mouse. The code works for usb keyboard, and works for mouse in the virtual machine.

b) i'm running the code during the time i do not touch keyboard and mouse, just guess the race conditions do not happen. For the worst case, even it sometimes happens, i just guess it should work for some cases that race conditions do not happen.

c) yes, there are APIs that windows provides to simulate mouse/key, such as mouse_event and keybd_event, but they are 'disabled'/shield by my target application, so i have to simulate at direct port access level.

d) sorry for the wrong posting. I just thought it is not relative to windows GUI, but the underlying hardware port direct access method. I saw something similar topics in the wiki and so asked here.

thanks again, and sorry.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to simulate mouse click in win7 with direct port acc

Post by Combuster »

Obviously you're trying to hack something.

The guaranteed course of action is to write your own virtual mouse driver so that the app can't detect differences in events between the two.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
guoguo
Posts: 5
Joined: Tue Aug 26, 2014 7:50 pm

Re: how to simulate mouse click in win7 with direct port acc

Post by guoguo »

hi Combuster, do you mean to write a filter driver for the mouse? it looks like too big ... :(
guoguo
Posts: 5
Joined: Tue Aug 26, 2014 7:50 pm

Re: how to simulate mouse click in win7 with direct port acc

Post by guoguo »

i tried win7 64bit in VirtaulBox on the same machine, the mouse click also works ...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: how to simulate mouse click in win7 with direct port acc

Post by Brendan »

Hi,
guoguo wrote:i tried win7 64bit in VirtaulBox on the same machine, the mouse click also works ...
So what you're saying is that, when running inside VirtualBox (which emulates PS/2 keyboard and mouse by default) your code (which assumes PS/2 keyboard and mouse) "works"; and when running on real hardware (that most likely uses USB keyboard and mouse) your code (which doesn't support USB) doesn't work?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
guoguo
Posts: 5
Joined: Tue Aug 26, 2014 7:50 pm

Re: how to simulate mouse click in win7 with direct port acc

Post by guoguo »

Hi Brendan,

yes, exactly as you said.

some more information, in 'Computer Management' -- 'Device Manager'

Inside the virtual machine, there are:
'Keyboards'
--Standard PS/2 Keyboard
'Mice and other pointing devices'
--HID-compliant mouse
--Microsoft PS/2 Mouse

Inside the host OS running on the real hardware, there are:
'Keyboards'
--Standard PS/2 Keyboard
'Mice and other pointing devices'
--HID-compliant mouse
--ThinkPad UltraNav Pointing Device

It explains that why the keyboard press works on both the host machine and the virtual machine, also explains why the mouse click works on the virtual machine. But, as mentioned in my original post, i did see the mouse click works on my host machine in a late night, very stable, but, it no longer works the next day....

Looks like that the direction is to add a 'virtual' PS/2 mouse in my system, is there such a way ...
Post Reply