Mouse interact with dos batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Mouse interact with dos batch

#16 Post by Aacini » 23 Jun 2014 13:57

Ok. Assume this example: there is INPUT.EXE program that works as interface to the mouse. As soon as the user click a mouse button, INPUT.EXE send to its Stdout a line with the position of the click.

On the other side, there is OTHER.BAT file that expect to read the mouse click and work with it, so these programs are executed as you indicated above:

Code: Select all

INPUT.EXE | OTHER.BAT

If OTHER.BAT get its input via SET /P, then it will wait at that command until the user give a mouse click. There is no way to avoid this problem, even if the Batch file uses a VBScript/JScript code to read the output of INPUT.EXE. As far as I know, there is no way in WSH to test if there is an input ready in Stdin...

I apologize for all these details, but I need to know that the proposed solution will work.

Antonio

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Mouse interact with dos batch

#17 Post by einstein1969 » 23 Jun 2014 17:00

Aacini wrote:Ok. Assume this example: there is INPUT.EXE program that works as interface with the mouse. As soon as the user click a mouse button, INPUT.EXE send to its Stdout a line with the position of the click.

On the other side, there is OTHER.BAT file that expect to read the mouse click and work with it, so these programs are executed as you indicated above:

Code: Select all

INPUT.EXE | OTHER.BAT

If OTHER.BAT get its input via SET /P, then it will wait at that command until the user give a mouse click. There is no way to avoid this problem, even if the Batch file uses a VBScript/JScript code to read the output of INPUT.EXE. As far as I know, there is no way in WSH to test if there is an input ready in Stdin...

Excuse me for these details, but I need to know that the proposed solution will work.

Antonio


Antonio,

When I refer of set/P problem i refer at the problem that the set/P is not reliable.

However if see the snake game of dbenham you can see that use two process. And the process that get the final input is not blocked even the process that give information on last input wait for a lot.

But there are other solution. For example include an HEARTBEAT, or include in the INPUT the output of "NO CHANGE" every few time (but is no preferable i think ... should be tested for probe my tesis).

PS:You use deductive method for know if a problem is solvable. I prefer the inductive method.

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Mouse interact with dos batch

#18 Post by carlos » 24 Jun 2014 21:35

Einstein I updated the mouse.com to version 1.1
I fix the problem. Apparentely, the mouse input are stored like a stack, then sucessive more clic, are readed the next time the program start and it end with previous version. I added a little fix for this and also I changed the output with format for use with set command.

Edit: I tested in windows 7 but if I run from command prompt the first time it run ok, but the second time, it not return. I will try fix for windows 7.

Edit 2: Published version 1.2. It works ok on freedos, windows xp x86 and windows x86 (Fix all the previous problems).[/b]

Edit 3: I found that the ouput of row columns is incorrect. Currently I fixed this, but I need wait until night for upload to the server. It will be the version 1.3

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Mouse interact with dos batch

#19 Post by carlos » 25 Jun 2014 20:40

I uploaded the version 1.3 to my site that have all fixed and also use the index begin in 1.
Also, I left it for create using debug here:
viewtopic.php?p=35371#p35371

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Mouse interact with dos batch

#20 Post by einstein1969 » 28 Jun 2014 07:36

Hi Carlos,

I have tried the version 1.3. This version work well.

But if I want reduce the CPU usage or use a mouse for drawing this is not optimal.

Few questions:

I have searched for interrupt to wait/sleep/delay but i have don't found any that work.

- It is possible add a delay in the polling cicle?

- In alternative it is possible return immediatly and capture the click?

- It is possible return current coordinates? Even if you have not clicked on anything?

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Mouse interact with dos batch

#21 Post by carlos » 29 Jun 2014 20:42

einstein.
Maybe all it can be done.
But I'm not a expert in this old programming and not have time for write it. This weekend I write more code for the next bg. I hope done it in the next two weeks. I only have free time for coding in the weekend.

Maybe write some code for create a .com is easy , but the testing time is so expensive.
For the mouse.com v1.3 I write many versions until I found the correct code that works in freedos, winxp and win7. With every change, you need test in all the systems.

I read that exists a event handler. Almost 3 interruptions with int 33h:
0Ch (I read that with this you need remove this handler before quit the program) calling these function with CX : 0
14H
18H

Then I think that the steps would be:
add the mouse handler address
wait:
sleep
goto wait
mouse_handler:
;do something

But I'm not sure of it will work.

The list of interruption functions is here:

Code: Select all

http://www.ctyme.com/intr/int-33.htm



Maybe is more practical programming it using win32 functions. Is more easy.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Mouse interact with dos batch

#22 Post by einstein1969 » 30 Jun 2014 19:54

carlos wrote:einstein.
Maybe all it can be done.
But I'm not a expert in this old programming and not have time for write it. This weekend I write more code for the next bg. I hope done it in the next two weeks. I only have free time for coding in the weekend.

Maybe write some code for create a .com is easy , but the testing time is so expensive.
For the mouse.com v1.3 I write many versions until I found the correct code that works in freedos, winxp and win7. With every change, you need test in all the systems.

I read that exists a event handler. Almost 3 interruptions with int 33h:
0Ch (I read that with this you need remove this handler before quit the program) calling these function with CX : 0
14H
18H

Then I think that the steps would be:
add the mouse handler address
wait:
sleep
goto wait
mouse_handler:
;do something

But I'm not sure of it will work.

The list of interruption functions is here:

Code: Select all

http://www.ctyme.com/intr/int-33.htm



Maybe is more practical programming it using win32 functions. Is more easy.


Thanks Carlos. I'm preparing an batch program that draw on screen for testing.
This use a virtual mouse (simulated via keyboard) or mouse.


@Aacini & @Carlos
For better interation I have made a HEARTBEAT that work with ping (or/and pathping).

This is an example of 22 tick at second HEARTBEAT.

From line of command:

Code: Select all

>cmd /v:on /q /c" for /L %n in (0) do ( (pathping -n -q 1 -p 1000 127.0.0.1 >nul) & start "PING" /B ping 127.0.0.1 -n 23 )  "  | find "128" | cmd /v:on /q /c" for /l %n in (0) do (set/p "L=" & if '!L:~-3!'=='128' echo(.) " | cmd /v:on /q /c" for /L %n in (0) do (set/p "L=" & set /a t=1!time:~6,2!!time:~9,2!-10000, dt=t-oldt, odt=oldt-o2, o2=oldt, oldt=t, m=(dt+odt^)*10/2 >NUL & set/p ".=!m! "<nul & if !m! equ 0 echo END ) "


How to use?

Code: Select all

HEARTBEAT | INPUT/MOUSE | OTHER


In INPUT or MOUSE (in this case mouse.com) there is a code like this:

Code: Select all

:loop
   wait for input (from heartbeat)
   check mouse coordinates and button status
   print coordinates and button status
goto :loop


It is possible even insert a condition for a while/repeat until loop.

The cpu usage is very low. The Hearbeat use less than 5% on monocore.

EDIT:@Carlos
The event handler is nice. It's an alternative at the hearbeat.

einstein1969

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Mouse interact with dos batch

#23 Post by einstein1969 » 01 Jul 2014 05:44

I have found this code that move the mouse on 0,0 and then print the coordinates of current mouse position in powershell.

The problem is that cover the full screen and not the dos windows.

Code: Select all

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(0,0)

$i=1
for ($i -le 5; $i++)
{
 Start-Sleep -m 500      #added delay
 $dY = ([System.Windows.Forms.Cursor]::Position.Y ) #read the Y coordinates
 $dX = ([System.Windows.Forms.Cursor]::Position.X ) #read the X coordinates
 Write-Host $dX,$dY     #print to screen
}


einstein1969

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Mouse interact with dos batch

#24 Post by einstein1969 » 08 Jul 2014 08:14

Hi,

I have found a workaround from a problem of debug under windows 7 32 bit.

I prefer using a assembler and not the opcode for write batch. (if possible, for Keep me far away as possible from the ISA )

This code wait until a LEFT mouse click is detected and print the register status:

Code: Select all

@echo off
(
  echo A

  echo MOV  AX,0005   ;100 RETURN BUTTON PRESS DATA, RETURN CX=COLUMN, DX=ROW
  echo XOR  BX,BX     ;    LEFT BUTTON
  echo INT  33
  echo CMP  BX,0000   ;    IF NOT BUTTON PRESSED ...
  echo JZ   100       ;    JUMP 100
  echo RET            ;10C

  echo(
  echo g10C
  echo q
)  | debug


but DON'T WORK! It block or crashes

the workaround is using the redirect on file:

Code: Select all

(
  echo A

  echo MOV  AX,0005   ;100 RETURN BUTTON PRESS DATA, RETURN CX=COLUMN, DX=ROW
  echo XOR  BX,BX     ;    LEFT BUTTON
  echo INT  33
  echo CMP  BX,0000   ;    IF NOT BUTTON PRESSED ...
  echo JZ   100       ;    JUMP 100
  echo RET            ;10C

  echo(
  echo g10C
  echo q
) >in.debug

debug < in.debug


This work well.

output:

Code: Select all

>mouse.cmd
-A
1391:0100 MOV  AX,0005   ;100 RETURN BUTTON PRESS DATA, RETURN CX=COLUMN, DX=ROW

1391:0103 XOR  BX,BX     ;    LEFT BUTTON
1391:0105 INT  33
1391:0107 CMP  BX,0000   ;    IF NOT BUTTON PRESSED ...
1391:010A JZ   100       ;    JUMP 100
1391:010C RET            ;10C
1391:010D
-g10C

AX=0001  BX=0001  CX=0010  DX=0008  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=1391  ES=1391  SS=1391  CS=1391  IP=010C   NV UP EI PL NZ NA PO NC
1391:010C C3            RET
-q


can anyone confirm?

einstein1969

prof101
Posts: 2
Joined: 09 Feb 2015 13:17

Re: Mouse interact with dos batch

#25 Post by prof101 » 19 Feb 2015 14:21

Ok so i'm working on a file that uses one button. But I cant figure out how to read a file I have called Mouse.exe. How would I read the contents of a .exe file.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Mouse interact with dos batch

#26 Post by npocmaka_ » 18 Mar 2016 14:53

https://github.com/npocmaka/batch.scrip ... /mouse.bat

Though it is not detecting mouse input.Still experimenting with this.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Mouse interact with dos batch

#27 Post by npocmaka_ » 21 Mar 2016 10:48

Here are some examples :


Code: Select all

//clicks at the current position
call mouse click

//double clicks at the current position
call mouse doubleClick

//right clicks at the current position
call mouse rightClick

//returns the position of the cursor
call mouse position

//scrolls up the mouse wheel with 1500 units
call mouse scrollUp 150

//scrolls down with 100 postitions
call mouse scrollDown 100

 //relatively(from the current position) moves the mouse with 100 horizontal and 100 vertial postitions
call mouse moveBy 100x100

//absolute positioning
call mouse moveTo 100x100

//relative drag (leftclick and move)
call mouse dragBy 300x200

//absolute drag
call mouse dragTo 500x500



May be a little explanation is needed?
C# and VB allows direct access to native functions with DLLExport attribute - you need to know the function name ,its argument types and then you can use it.
So far it looks straight forward . First obstacle is the marshaling - the datatypes could vary between 32b and 64b machines and as a whole windows uses strange custom data types not specific for c/c++.

Here's a you can see how need to map your data types -

Code: Select all

 https://msdn.microsoft.com/en-us/library/fzhhdwae(v=vs.110).aspx

usually the documentation what you'll need you can find in msdn. For the mouse.bat most important was the SendInput function

Code: Select all

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx  


Often in windows APIs a whole structures are used as arguments and you'll need to create/marshal a composite datatypes which according to me could be most difficult part in calling native functions from C#.

And also often there's a passing by reference instead of returning object (I suppose this improves the performance as eliminates the need of creating a whole new object to be returned)

Here's a whole library that wraps user32.dll (this where keyboard and mouse functions are located) :

Code: Select all

https://inputsimulator.codeplex.com/


Elegant written and flexible though for hackish scripts like the mouse.bat I need single file solutions...

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Mouse interact with dos batch

#28 Post by npocmaka_ » 21 Mar 2016 10:52

Now I'm writing a mouse sniffer (almost done).
And very fast I've found I need a way disable the quick edit mode.

So this became a spin-off script as it looks like something that could be useful in more situations : - https://github.com/npocmaka/batch.scrip ... ckEdit.bat

usage :

Code: Select all

Enable:
 quickEdit  1
Disable:
 quickEdit  2
Get State:
 quickEdit  3


Pretty straightforward usage of functions in kernel32.dll

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Mouse interact with dos batch

#29 Post by npocmaka_ » 22 Mar 2016 03:17

https://github.com/npocmaka/batch.scrip ... niffer.bat

Mouse sniffer it should print the left button mouse clicks coordinates,timestamp and true or false depending if the click is within the command prompt window.
Not heavily tested yet.

Post Reply