How can i create my mouse click event in std-c++

I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;
int main(){

asm("mov ax,0x03"
"int 0x33"
"test bl,1"
"jnz Left_pressed_button"
"test bl,2"
"jnz Right_pressed_button"
);
system("PAUSE");
}


But i take this error:
1
2
  Assembler messages: 
  too many memory references for `mov'  
If you want to write in assembly then do it. If you want to write in C++ then do that. Just because you CAN use SOME assembly in SOME compilers (GCC says specifically not to do this with their software) it doesn't mean you should.

Where did you even get this code from?
closed account (3hM2Nwbp)
What compiler are you using? How to inline assembler varies with compiler. If you're using Visual Studio then it would be something similar to this:

1
2
3
4
5
6
7
8
__asm{
	mov ax,0x03
	int 0x33
	test bl,1
	;jnz Left_pressed_button
	test bl,2
	;jnz Right_pressed_button
}


* Though I'm 99.999% sure it won't do what you want it to.
Last edited on
You cannot communicate with devices directly in any modern operating system.
Use the appropriate interfaces that your OS provides for this.
MingW.My IDE ise Dev-c++
I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#define Mouse 0x33
#define left 1
#define right 2

void ActivMouse()
  {     _AX=32;
        geninterrupt(Mouse);
  }




int main(){
    ActivMouse();
if(left){
             printf("Left pressed");   
                }
if(right)
printf("Right pressed");
system("PAUSE");}


Errors:
1
2
10 C:\Dev-Cpp\lib\mouseexam.cpp `_AX' undeclared (first use this function) 
11 C:\Dev-Cpp\lib\mouseexam.cpp `geninterrupt' undeclared (first use this function) 


This is a solution for windows.

http://msdn.microsoft.com/en-us/library/ms646260%28v=vs.85%29.aspx
The above link is MSDN documentation for a windows function mouse_event()
1
2
3
4
5
6
7
VOID WINAPI mouse_event(
  __in  DWORD dwFlags,
  __in  DWORD dx,
  __in  DWORD dy,
  __in  DWORD dwData,
  __in  ULONG_PTR dwExtraInfo
);


You could use it like

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <windows.h>

void left_click()
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, x_pos, y_pos, 0, 0);
    mouse_event(MOUSEEVENTF_LEFLTUP, x_pos, y_pos, 0, 0);
}

void right_click()
{
    mouse_event(MOUSEEVENTF_RIGHTDOWN, x_pos, y_pos, 0, 0);
    mouse_event(MOUSEEVENTF_RIGHTUP, x_pos, y_pos, 0, 0);
}

Something like that should work, though, note that it's platform dependent.




Last edited on
Thanks.I know this method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
HANDLE a;
DWORD b;
INPUT_RECORD c[1];
if(HANDLE(a=GetStdHandle(0xfffffff6))==INVALID_HANDLE_VALUE)
return 1;
while(1){
if(!ReadConsoleInput(a,c,1,&b))
return 1;
for(DWORD i=0;i<b;i++)
if(c[i].EventType==2){
if(c[i].Event.MouseEvent.dwButtonState&0x0001)
cout<<"Left click\n";
if(c[i].Event.MouseEvent.dwButtonState&0x0002)
cout<<"Right click\n";
}
}
return 0;}


My sweated point all PC mostly use in x86 microcontrollers.But doesn't cross-platform mouse funcs(drivers).Thats very interesting.
Last edited on
It won't be cross platform because what you are doing is telling the OS to act like the mouse has done something. Since every part of that from the sensing to the dispatch of the instruction would be handled by the OS, the code needs to be OS specific.

There are some clever things you can do to (f\m)ake this cross platform, but those are mostly preprocessor instructions and macros defining a torent of ifndefs to handle where instructions go and what globals are defined at run time. In my opinion since you end up writing everything twice it's not really worth the trouble.
Last edited on
Topic archived. No new replies allowed.