按下鼠标两次后退出的汇编程序

Assembly program that quits after mouse has been pressed twice

所以现在我只能让它循环并等待点击,但是,一旦我点击,程序认为我仍然按住点击或其他东西,因为按钮状态永远不会改变一次我点击了。所以程序只是循环到无穷大。

这是我的:

.286
TITLE ''
.MODEL SMALL

.STACK

.DATA
    clickR DB 'Right click',10,13,'$'
    ; clickI DB 'Left click',10,13,'$'

.CODE



main PROC FAR
    MOV AX, @DATA
    MOV DS, AX

    MOV AX, 0h       ; Init mouse
    INT 33h

    MOV AX, 01h      ; Show pointer
    INT 33h

mouse:
    MOV AX, 05h      ; Query Button-Pressed Counter
    MOV BX, 00h      ; Left button
    INT 33h

    CMP AX, 01h      ; Check left button status
    JNE mouse        ; Not pressed? Go back
    CMP BX, 02h      ; Check if it has been pressed twice
    JB right         ; < 2? Go to label
    JAE quit         ; >= 2? quit

    JMP mouse

right:
    MOV AH, 09h       ; Print that r-click has been pressed
    LEA DX, clickR
    INT 21h
    JMP mouse         ; Go back to mouse loop

quit:
    MOV AX, 4C00h
    INT 21h
main ENDP

END main

使用 AH=3 RETURN POSITION AND BUTTON STATUS 代替 AH=5

当它returns BX=1(左键按下)时,重复查询并等待BX=0(按钮弹起)。

如果需要检测双击,请再次重复这两个查询。

CMP AX, 01h      ; Check left button status
JNE mouse        ; Not pressed? Go back

如果要等待 2 次点击,您还必须允许释放按钮!上面的代码使这成为不可能,因为您坚持要按下按钮。

下一个代码等待鼠标左键点击 2 次并退出:

    MOV AX, 0000h    ; Init mouse
    INT 33h
    MOV AX, 0001h    ; Show pointer
    INT 33h
mouse:
    MOV AX, 0005h    ; Query Button-Pressed Counter
    XOR BX, BX       ; Left button
    INT 33h
    CMP BX, 2        ; Check if it has been pressed twice
    JB  mouse         ; < 2? Go to label
quit:
    MOV AX, 4C00h
    INT 21h

根据vitsoft所说的解决方案

现在,这是我修复它的方法(它可能不是最优的,但它有效):

.286
TITLE ''
.MODEL SMALL

.STACK

.DATA
    clickR DB 'Right click',10,13,'$'
    released DB 'Released',10,13,'$'

.CODE

rightC PROC NEAR
    MOV AH, 09h
    LEA DX, clickR
    INT 21h
    RET
rightC ENDP

releaseStr PROC NEAR
    MOV AH, 09h
    LEA DX, released
    INT 21h
    RET
releaseStr ENDP


main PROC FAR
    MOV AX, @DATA
    MOV DS, AX

    MOV AX, 0h       ; Init mouse
    INT 33h

    MOV AX, 01h      ; Show pointer
    INT 33h

    XOR CX, CX       ; Init counter
    PUSH CX          ; Save counter

mouse:
    MOV AX, 03h      ;  Query Position / Button Status
    INT 33h

    CMP BX, 01h      ; Check left button status
    JNE mouse        ; Not pressed? Go back
    CALL rightC      ; Print that r-click has been pressed

release:
    MOV AX, 03h      ; Query Position / Button Status
    INT 33h

    CMP BX, 00h      ; Check button status (None)
    JNE release      ; Still no released? Go back
    CALL releaseStr  ; Print that r-click has been released

    POP CX           ; Return counter
    INC CX           ; Counter + 1 = r-click has been pressed and released one time
    CMP CX, 02h      ; Check if this has happened twice
    JAE quit         ; Yes? quit
    PUSH CX          ; No? Save counter

    JMP mouse        ; Go back to mouse loop

quit:
    MOV AX, 4C00h
    INT 21h
main ENDP

END main