Reading mouse ports

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
tcaud
Newbie
Posts: 5
Joined: Fri Feb 19, 2016 6:40 am

Reading mouse ports

Post by tcaud »

I realized it was probably possible to create an ASM-less method of accessing the mouse by polling it directly. This is theoretically straight forward, but it seems like something's in the way. The port (0x60) is always reading either 21 or 28.

Running under WinXP SP3 32-bit. (Dosbox doesn't do real mouse simulation).

Code: Select all

DIM mouseBytes(2) AS INTEGER
byteCount% = 0

WAIT &H64, 2
OUT &H64, &HD4
OUT &H60, &HF4

DO UNTIL INKEY$ = "Q"

    PRINT INP(&H64)

	WAIT &H64, 2
	OUT &H64, &HD4
	OUT &H60, &HF5

    IF INP(&H64) AND &H20 THEN
                        
        PRINT "Mouse active..."

        mouseBytes(byteCount%) = INP(&H60)
        byteCount% = byteCount% + 1

        IF byteCount% = 3 THEN

            IF mouseBytes(0) = &H80 OR mouseBytes(0) = &H40 THEN
            ELSE

                'PRINT "Byte 3"

                'xDifference = (mouseBytes(0) AND 3) * 64 + mouseBytes%(1)
                'yDifference = (mouseBytes(0) AND 12) * 16 + mouseBytes%(2)

                'IF xDifference% >= 128 THEN :xDifference% = xDifference% - 256
                'IF yDifference% >= 128 THEN :yDifference% = yDifference% - 256
                '_MOUSEX% = _MOUSEX% + xDifference
                'mouseY% = mouseY% + yDifference
                mouseBtn1% = mouseBytes%(0) AND 32
                mouseBtn2% = mouseBytes%(0) AND 16
                PRINT "Byte: ", mouseBytes%(0), ", Status 1:", mouseBtn1%, ", Status 2:", mouseBtn2%
                SLEEP 1
            END IF
            byteCount% = 0
           
        END IF
    END IF
LOOP
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Reading mouse ports

Post by burger2227 »

mouseBytes(byteCount%) = INP(&H60)
INP(96) is reading the keyboard into an array that never prints.

http://www.qb64.net/wiki/index.php/INP
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
tcaud
Newbie
Posts: 5
Joined: Fri Feb 19, 2016 6:40 am

Re: Reading mouse ports

Post by tcaud »

I looked into it some more... the problems are intractible in the modern age. Neither Dosbox or NTVDM accurately simulate the PS/2 mouse port, nor do they map USB mice to that port. The only way to access the mouse, now, is by calling the IRQ.

Here's the corrected code. It is correct, based on the DOS port docs I've found, but it doesn't work because NTVDM doesn't accept the write to get the mouse status (0xE5 at 0x60). That port 0x64, the status read port, doesn't set the mouse read flag at all is further evidence NTVDM doesn't accurately manage the mouse.

Code: Select all

mouseStatus% = 0
DO
sleep 1
WAIT &H64, 0 'wait for port clear
OUT &H60, &HE9	'request mouse status
WAIT &H64, 1	'wait for port ready
mouseStatus% = INP(&H60) AND 1	' get mouse status bytes
mouseStatus2% = INP(&H60)
LOOP UNTIL mouseStatus% > 0 OR INKEY$ = "Q"
PRINT mouseStatus%
END
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Reading mouse ports

Post by burger2227 »

This code returns 1 and 129 in QB64 and changes with key presses, not mouse:

Code: Select all

mouseStatus% = 0
DO
    SLEEP 1
    WAIT &H64, 0 'wait for port clear
    OUT &H60, &HE9 'request mouse status
    WAIT &H64, 1 'wait for port ready
    mouseStatus% = INP(&H60) AND 1 ' get mouse status bytes
    mouseStatus2% = INP(&H60)

    PRINT mouseStatus%; mouseStatus2%;
LOOP UNTIL INKEY$ = "q"
END

Use CALL Absolute in QB. That should still work.

http://www.qb64.net/wiki/index.php/CALL_ABSOLUTE
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
tcaud
Newbie
Posts: 5
Joined: Fri Feb 19, 2016 6:40 am

Re: Reading mouse ports

Post by tcaud »

The keyboard and the mouse read from the same port. And yes, Call ABSOLUTE works everywhere.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Reading mouse ports

Post by burger2227 »

I have never used INP(&H60) to read a mouse! Where are you getting such information?
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
tcaud
Newbie
Posts: 5
Joined: Fri Feb 19, 2016 6:40 am

Re: Reading mouse ports

Post by tcaud »

User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Reading mouse ports

Post by burger2227 »

No, those settings are probably blocked on NT. They SET mouse and keyboard settings or read them.

Note that there is nothing listed for the mouse coordinates, just buttons.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply