Mouse interact with dos batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Mouse interact with dos batch

#1 Post by einstein1969 » 21 Jun 2014 10:03

Hello to all

I need to know what are the methods for interacting with the mouse in dos batch.

I'd like to know all known methods with VBScript / JScript or mshta or powershel or rundll. (But without aid EXCEL)

Programs compiled is not my interest (the easy way).

the ASM approach is interesting if I can make an EXE from DOS batch with few or no tools.

The method of debug.exe is only partial (32bit platform) for me. But I'm interested to know.

einstein1969

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

Re: Mouse interact with dos batch

#2 Post by npocmaka_ » 21 Jun 2014 10:52

Is the .NET an option (as you listed also the powershell)?
It requires compiled exe but has less prerequisites than powershell (powershell is not installed by default on Vista and XP , and I think in Base editions of win 7 and 8 ) as its hard to find even an old XP machine without .net framework .

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

Re: Mouse interact with dos batch

#3 Post by einstein1969 » 21 Jun 2014 12:50

npocmaka_ wrote:Is the .NET an option (as you listed also the powershell)?
It requires compiled exe but has less prerequisites than powershell (powershell is not installed by default on Vista and XP , and I think in Base editions of win 7 and 8 ) as its hard to find even an old XP machine without .net framework .


I would still keep me within the scripting and not compiled. It 's true that .NET is similar to java so it is a hybrid ...
But I don't know very well .NET. So i don't know the answer at this question. :(

Post your solution anyway.

Thanks npocmaka_ for your question.

einstein1969

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

Re: Mouse interact with dos batch

#4 Post by carlos » 21 Jun 2014 15:16

einstein.
Is not the method of debug.
The tool you use for rebuild a binary file is independant of the type of program that you rebuild.
Using bhx I can rebuild a 16 bit program or 32 bit program or any.

Whay you means say is if the program is for 16 bit it will use dos interruptions.
See for example:

Code: Select all

http://stanislavs.org/helppc/int_33.html

A 32 bit program will use the win32 api (calls to libraries like msvcrt.dll)

For a 16 bit .com program you can see for example this from Mike Jones:

Code: Select all

http://2dos.homepage.dk/batutil/NEWS5.HTM#mouse

But it use debug for run (g command) and inspect the registers (that are like variables). But if you modify the program you can print the content of the that registers that have the cell info where you clic. In that case you can use this mouse routine without debug.

It will run under emulation for 16 bits only in 32 bits windows systems, and will not run in 64 bits windows systems.

But there are not a only one method for do the things, because in programming you can do the same thing in different ways.

Aboud a c routine using win32 for get the mouse input I have one in my bg program. See this:

Code: Select all

http://consolesoft.com/p/bg/bg.txt
under void mouse(void)

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

Re: Mouse interact with dos batch

#5 Post by carlos » 21 Jun 2014 17:04

About the program of Mike Jones. This is the assembly source:
I add some comments, for understand it.

Show mouse cursor AX=0001
http://www.delorie.com/djgpp/doc/rbinter/id/76/59.html

Return mouse and button status AX=0003
http://www.delorie.com/djgpp/doc/rbinter/id/78/59.html

Code: Select all

C:\>debug
-e100 B8 1 0'3'DB CD'3'B0 3'C'CD'3'B DB't'FA 91 D3 EA D3 E8 91 C3
-u100
0C9E:0100 B80100        MOV     AX,0001 ;set AX=0001 ;AH=00 AL=01
0C9E:0103 33DB          XOR     BX,BX ;set BX=0000
0C9E:0105 CD33          INT     33 ;Show Mouse Cursor AX=0001
0C9E:0107 B003          MOV     AL,03 ;set AL=03; in this case AX=0003
0C9E:0109 43            INC     BX ;set BX=BX+1
0C9E:010A CD33          INT     33 ;Return mouse and button status AX=0003
;;Return: BX = button status (see #03168)
;;   CX = column
;;   DX = row
;;
;;#03168 :
;;Bitfields for mouse button status:
;;Bit(s) Description )
;; 0   left button pressed if 1
;; 1   right button pressed if 1
;; 2   middle button pressed if 1
;;
;; If left button was pressed BX in binary would be:
;; 0000000000000001
;; If right button was pressed BX in binary would be:
;; 0000000000000010
;; If none button was pressed BX in binary would be:
;; 0000000000000000
;
;;
0C9E:010C 0BDB          OR      BX,BX ;;
0C9E:010E 74FA          JZ      010A ;;if none mouse button pressed try again
;;some data processing of BX,CX,DX:
0C9E:0110 91            XCHG    CX,AX ;;Swap CX AX
0C9E:0111 D3EA          SHR     DX,CL ;;DX= DX>>CL ;;unsigned
0C9E:0113 D3E8          SHR     AX,CL ;;AX=AX>>CL ;;unsigned
0C9E:0115 91            XCHG    CX,AX ;;Swap CX AX
;;
;;Here you can add instructions for print BX,CX,DX
;;
0C9E:0116 C3            RET


When you run it, and you do a clic.
The register BX will have 1 for left clic button and 2 for right clic button. CX will have the X coord of the cell. DX will have the Y coord of the cell.

For example If I clic in cell(2,2) with left clic debug will print this:

Code: Select all

-e100 B8 1 0'3'DB CD'3'B0 3'C'CD'3'B DB't'FA 91 D3 EA D3 E8 91 C3

-g116


AX=0003  BX=0001  CX=0002  DX=0002  SP=FFEE  BP=0000  SI=0000  DI=0000 
DS=0C9E  ES=0C9E  SS=0C9E  CS=0C9E  IP=0116   NV UP EI PL NZ NA PO NC
0C9E:0116 C3            RET                                      
-q


I think that these two instructions are unnecesary:

Code: Select all

0C9E:0103 33DB          XOR     BX,BX ;set BX=0000

Code: Select all

0C9E:0109 43            INC     BX ;set BX=BX+1


because in the calls to int 33, bx is not used like a parameter.

I wiil try write a complete routine for mouse based on this.

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

Re: Mouse interact with dos batch

#6 Post by Aacini » 21 Jun 2014 17:41

A small problem with this type of interaction (a Batch file calling an .exe program to check the mouse) is that the .exe must return to the Batch immediately, so the Batch file must be continually pooling the .exe and respond as fast as possible to a mouse click. This problem usually don't happen when the .exe is the same program that control the mouse, because it is usually fast enough to do that with no problems. I already wrote and posted such mouse management auxiliary program. See this post:

Aacini wrote:I also wrote a new auxiliary program for this occasion; this program, called GetInput.exe, allows to get input from both keyboard and mouse and return it via errorlevel. A key press is returned as a positive value and a mouse click as a negative value that indicate the mouse cursor position in the screen using this formula: errorlevel = -(line<<16 + column)...

Just for a change, I included the assembly source code of this program below.

You may review such program and see if it is enough for your needs; otherwise, I could modify it a little...

Antonio

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

Re: Mouse interact with dos batch

#7 Post by carlos » 21 Jun 2014 20:23

Aacini. Your program is good.
I will adapt it to c and use in my bg tool in a function that can get input from mouse or keyboard.


Einstein, I programmed the mouse routine for dos adapting the above code from Mike Jones. I modify for only allow the primary button clic, and also I write the complete routine for print the content of a 16 bit register in decimal format.

Here you found:

Code: Select all

http://consolesoft.com/old-school/

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

Re: Mouse interact with dos batch

#8 Post by einstein1969 » 22 Jun 2014 08:56

@Carlos, @Aacini

very nice works.

I'm studying what you have done to see if it meets my requirements.

First is How can I create the .COM?

Facts:
- With the source and DEBUG.EXE i can create the .COM or use directly with DEBUG.EXE without create .COM.
- It's possible create .COM or execute the asm(with DEBUG.exe) only on 32bit system because DEBUG.EXE is only on 32bit.

The .COM don't work on 64 bit system.

How convert .COM in .EXE?

Next I will create a simple application for testing this methods.

einstein1969

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

Re: Mouse interact with dos batch

#9 Post by carlos » 22 Jun 2014 12:39

I create the .com with fasmw. Also you can create with debug using the A instruction (where you put the instructions like assembly code):

Code: Select all

A 100 RET
or you can use the directly byte injection, the byte for the opcode RET is C3, then you can put:

Code: Select all

E 100 C3
But I prefer program it in fasm, then when is ready, you can choose the preferred method for rebuild with debug. In debug you cannot use labels, you cannot use "jmp mylabel", you put "jmp address" for example, and because you are programming the code this address can change, and you need update it.

Edit: Updated. This is the version 1.3 of mouse.com
This is the rebuild of my mouse.com using debug:

Code: Select all

@ECHO OFF
REM BUILD MOUSE.COM V1.3
REM ASSEMBLY SOURCE WITH COMMENTS HERE: http://consolesoft.com/old-school/
IF EXIST MOUSE.COM DEL MOUSE.COM
IF EXIST MOUSE.DEB DEL MOUSE.DEB
ECHO NMOUSE.COM>MOUSE.DEB
ECHO E0100 B8 01 00 CD 33 B8 05 00 31 DB CD 33 B8 05 00 31 >>MOUSE.DEB
ECHO E0110 DB CD 33 83 F8 01 75 F4 83 FB 00 74 EF C1 EA 03 >>MOUSE.DEB
ECHO E0120 C1 E9 03 42 41 52 B4 09 BA 76 01 CD 21 5A 89 D0 >>MOUSE.DEB
ECHO E0130 E8 1A 00 B4 06 B2 20 CD 21 89 C8 E8 0F 00 B4 06 >>MOUSE.DEB
ECHO E0140 B2 0D CD 21 B2 0A CD 21 B8 00 4C CD 21 50 53 51 >>MOUSE.DEB
ECHO E0150 52 BB 0A 00 30 C9 31 D2 F7 F3 80 C2 30 52 FE C1 >>MOUSE.DEB
ECHO E0160 83 F8 00 75 F1 B4 06 5A CD 21 FE C9 80 F9 00 75 >>MOUSE.DEB
ECHO E0170 F6 5A 59 5B 58 C3 53 45 54 20 43 4C 49 43 3D 20 >>MOUSE.DEB
ECHO E0180 24 >>MOUSE.DEB
ECHO RCX >>MOUSE.DEB
ECHO 81 >>MOUSE.DEB
ECHO W >>MOUSE.DEB
ECHO Q >>MOUSE.DEB
TYPE MOUSE.DEB | DEBUG >NUL
DEL MOUSE.DEB



I programming it because in the past I use the routine of Mike Jones, and I always want use it without debug.
But today if I need a routine for the mouse I will use one that use win32, because 16 bits is obsolete. Today, the most systems are 64 bits. I think coming soon 32 bits anyways will be obsolete.
If you use the original routine of Mike Jones, you need use debug for charge the instructions in memory, run it, and with debug look the registers that have the info about the mouse clic. If you use my mouse.com you not need use debug for run it, because it print the info. My mouse.com I think is only useful in windows nt to windows xp 32 bits, because ms-dos not have a for /f, then I not know a way for catch the info displayed in the screen. I programming it only for fun :) I think modify the program for print SET CLIC= row col
Using that way you can redirect to a batfile and execute for have the info in a environment variable.

You can convert a .com to .exe but this .exe will have the exe format of 16 bits, and anyways will not run on 64 bits systems.
Last edited by carlos on 25 Jun 2014 20:39, edited 6 times in total.

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

Re: Mouse interact with dos batch

#10 Post by einstein1969 » 23 Jun 2014 02:55

@Carlos,

Thanks for the explanation. I realized many things.

The program written with DEBUG :

It has two things that do not go so well. One is the fact that it uses too much CPU in the control loop of mouse events.
You could avoid doing that cycle?

In addition, the part that prints a character on the screen. I understand that you can not capture the output?
Have you tried to use Ax = 2 (write to standard output) to INT21?

@Aacini
How does your program to wait without using the cpu?
It is possible to catch all event? pressed mouse, release mouse, change position? and print on standard output?
Another nice thing would be to continue printing the information without exiting the program in order to use it in multithreading.

einstein1969

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

Re: Mouse interact with dos batch

#11 Post by carlos » 23 Jun 2014 05:14

einstein, I not have performance problems with it.
I can capture the output, but I referring to old msdos or freedos where for /f is not present.
See, my edit in the above post, where I suggest modify it for print SET CLIC = row col
I modify it because is a method that works on the old msdos.
But for modern windows you can use the function in the bg or the codeof Aacini. If you look it, are similar in catch the mouse input. But aacini code also catch the keyboard input.

All will depend of the target system that you want. You want capture only a mouse clic or also the keyboard (Aacini program)?. You want the program wait until you do the clic, or you want catch the clic info only if it was present ?
All will depend of you needs.

I onlu program mouse.com for avoid use it with debug and because you want know the available methods. But today, I use the routine of my bg program.

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

Re: Mouse interact with dos batch

#12 Post by einstein1969 » 23 Jun 2014 06:33

carlos wrote:About the program of Mike Jones. This is the assembly source:
...

I think that these two instructions are unnecesary:

Code: Select all

0C9E:0103 33DB          XOR     BX,BX ;set BX=0000

Code: Select all

0C9E:0109 43            INC     BX ;set BX=BX+1


because in the calls to int 33, bx is not used like a parameter.

I wiil try write a complete routine for mouse based on this.


Carlos, The bx=0000 is necessary because if there no click the int 33 don't clear the BX and if there is old value the program exit with wrong info.

I have not tested the second INC BX...

EDIT: No, the problem is that the buffer input is not clear. Mouse.com Don't work well.

einstein1969

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

Re: Mouse interact with dos batch

#13 Post by Aacini » 23 Jun 2014 07:03

einstein1969 wrote:@Aacini
How does your program to wait without using the cpu?

My program use ReadConsoleInput Win API that put the process in a wait state until an input event has been read. The old DOS Int 33H method is entirely different.

einstein1969 wrote:It is possible to catch all event? pressed mouse, release mouse, change position?

Yes. See the listing of my GetInput.asm:

Code: Select all

check_mouse:
        cmp     lpBuffer.EventType, MOUSE_EVENT ;is a mouse event?
        jne     get_event                       ;no: ignore event
        ;                                       ;left or right button pressed?
        test    lpBuffer.MouseEvent.dwButtonState, FROM_LEFT_1ST_BUTTON_PRESSED OR RIGHTMOST_BUTTON_PRESSED
        jz      get_event                       ;no: ignore button releases


einstein1969 wrote:and print on standard output?

Another nice thing would be to continue printing the information without exiting the program in order to use it in multithreading.

einstein1969

Please, carefully read this paragraph:
Aacini wrote:A small problem with this type of interaction (a Batch file calling an .exe program to check the mouse) is that the .exe must return to the Batch immediately, so the Batch file must be continually pooling the .exe and respond as fast as possible to a mouse click.

In my example, the Batch code just read input events (keys pressed and mouse clicks) and display them in the screen. If you want that the Batch program do other things simultanously to get input from the mouse, like in an animated game (think of Dave's SNAKE.BAT, for example), then the approach must be different. For example:

Code: Select all

:nextInput
   rem Next command must return immediately; if there is not any input event, it returns zero
   GetInput
   set input=%errorlevel%
   if %input% gtr 0 (
      rem Key pressed: respond here to a key press
      . . . .
   ) else if %input% lss 0 (
      rem Mouse button clicked: respond here to a mouse click
      . . . .
   )
   rem Do here the usual business of this program
   rem For example, animate the snake...
   . . . .
if %input% neq %EndThisProgram% goto nextInput
goto :EOF

Of course, an .exe program can continuously print the mouse information on standard output as long as it does NOT ends!. This means that at the same time the Batch code does NOTHING because it is waiting for the mouse.exe program to end (as usual).

Although the mouse.exe program could be started via START /B command, so it will run simultanously with the Batch code, there must be a mechanism that allows the mouse.com program to send the information back to the Batch code. At first sight, I think we could use a synchronization mechanism similar to the one I used in my Batch-JScript Tee.bat hybrid script via a semaphore file, but I am afraid that this method will not be fast enough to be satisfactory in this case...

I suggest you to clearly state your request: define with detail what you want each part to do (that is, the mouse.exe program and the Batch code) so we can review if it is possible to develop a suitable solution.

Antonio

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

Re: Mouse interact with dos batch

#14 Post by carlos » 23 Jun 2014 07:54

einstein. I will look for fix at night. I test and the problem is that it run ok one time, but the second time it print the previous info.

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

Re: Mouse interact with dos batch

#15 Post by einstein1969 » 23 Jun 2014 09:59

Aacini wrote:
einstein1969 wrote:@Aacini
How does your program to wait without using the cpu?

My program use ... ReadConsoleInput Win API that put the process in a wait state until an input event has been read. The old DOS Int 33H method is entirely different.

einstein1969 wrote:It is possible to catch all event? pressed mouse, release mouse, change position?

Yes. See the listing of my GetInput.asm:

Code: Select all

check_mouse:
        cmp     lpBuffer.EventType, MOUSE_EVENT ;is a mouse event?
        jne     get_event                       ;no: ignore event
        ;                                       ;left or right button pressed?
        test    lpBuffer.MouseEvent.dwButtonState, FROM_LEFT_1ST_BUTTON_PRESSED OR RIGHTMOST_BUTTON_PRESSED
        jz      get_event                       ;no: ignore button releases


einstein1969 wrote:and print on standard output?

Another nice thing would be to continue printing the information without exiting the program in order to use it in multithreading.

einstein1969

Please, carefully read this paragraph:
Aacini wrote:A small problem with this type of interaction (a Batch file calling an .exe program to check the mouse) is that the .exe must return to the Batch immediately, so the Batch file must be continually pooling the .exe and respond as fast as possible to a mouse click.

In my example, the Batch code just read input events (keys pressed and mouse clicks) and display them in the screen. If you want that the Batch program do other things simultanously to get input from the mouse, like in an animated game (think of Dave's SNAKE.BAT, for example), then the approach must be different. For example:

Code: Select all

:nextInput
   rem Next command must return immediately; if there is not any input event, it returns zero
   GetInput
   set input=%errorlevel%
   if %input% gtr 0 (
      rem Key pressed: respond here to a key press
      . . . .
   ) else if %input% lss 0 (
      rem Mouse button clicked: respond here to a mouse click
      . . . .
   )
   rem Do here the usual business of this program
   rem For example, animate the snake...
   . . . .
if %input% neq %EndThisProgram% goto nextInput
goto :EOF

Of course, an .exe program can continuously print the mouse information on standard output as long as it does NOT ends!. This means that at the same time the Batch code does NOTHING because it is waiting for the mouse.exe program to end (as usual).

Although the mouse.exe program could be started via START /B command, so it will run simultanously with the Batch code, there must be a mechanism that allows the mouse.com program to send the information back to the Batch code. At first sight, I think we could use a synchronization mechanism similar to the one I used in my Batch-JScript ...Tee.bat hybrid script via a semaphore file, but I am afraid that this method will not be fast enough to be satisfactory in this case...

I suggest you to clearly state your request: define with detail what you want each part to do (that is, the mouse.exe program and the Batch code) so we can review if it is possible to develop a suitable solution.

Antonio


Thanks Antonio for response.

There are solution at every question you have descrived. But I don't want responce at every question you have descrived. If you need, ask me.

I want run the INPUT in a separate process:

Code: Select all

Start "" /B ....


or

Code: Select all

INPUT | OTHER ...


or other methods that can think at the moment. I will test this method and i will arrive at conclusion after the test. Something I have already tested.

In this two methods the process run Parallel and The first is ASYNC. The second method is SYNC if the OUTPUT of the left process is in input at right process.

The second method can also be BUFFERED and UNBUFFERED or partial buffered. There are some workaround for the set /P problem. (With WSH for example and without using semaphores).

The First method and second with a FIFO queue or RING BUFFER can be used for some application. This achieve low Latency and Low Cpu Usage (or controlled). Check this for info.

This is onother example using set/P with file and seem Async but i think that there is a SYNC mechanism that is not visible. I not investigate...

I need a utility that output a line (with LF/CR at the end or with a separator like ; ) (can be passed in input?) continuously.
This utility can end after N cycles/lines outputed (can be passed in input?)
The utility print only on change. (notify change).

einstein1969

Post Reply