Author Topic: Mouse demo using _DEVICE commands  (Read 4563 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Mouse demo using _DEVICE commands
« on: February 21, 2019, 06:15:28 am »
Code: QB64: [Select]
  1. DIM SHARED MouseX AS INTEGER, MouseY AS INTEGER
  2. DIM SHARED MouseWheel AS INTEGER
  3. DIM SHARED LeftMouse AS INTEGER, RightMouse AS INTEGER, MiddleMouse AS INTEGER
  4. DIM SHARED ClickThreshold AS SINGLE
  5. ClickThreshold = 0.2 'down and up in threshold of a second to count as a click.  If down for more then threshold, we just have a hold event.
  6.  
  7.  
  8. SCREEN _NEWIMAGE(640, 480, 32)
  9.  
  10.  
  11.  
  12.     _LIMIT 60
  13.  
  14.     UpdateMouseInfo
  15.     LOCATE 1, 1
  16.     PRINT MouseX, MouseY
  17.     PRINT MouseWheel
  18.     PRINT LeftMouse, MiddleMouse, RightMouse
  19.     IF LeftMouse AND 2 THEN leftclick = leftclick + 1
  20.     IF MiddleMouse AND 2 THEN middleclick = middleclick + 1
  21.     IF RightMouse AND 2 THEN rightclick = rightclick + 1
  22.     PRINT leftclick, middleclick, rightclick
  23.  
  24. LOOP UNTIL INKEY$ = CHR$(27) 'escape key exit
  25.  
  26. SUB UpdateMouseInfo
  27.     DIM SW AS INTEGER, SH AS INTEGER
  28.     DIM LM AS INTEGER, MM AS INTEGER, RM AS INTEGER
  29.  
  30.     STATIC leftdown AS SINGLE, middledown AS SINGLE, rightdown AS SINGLE
  31.  
  32.     WHILE _DEVICEINPUT(2): MouseWheel = MouseWheel + _WHEEL(3): WEND 'clear and update the mouse buffer
  33.  
  34.     SW = _WIDTH \ 2: SH = _HEIGHT \ 2
  35.     MouseX = _AXIS(1) * SW + SW: MouseY = _AXIS(2) * SH + SH
  36.  
  37.  
  38.  
  39.     LM = _BUTTON(1): MM = _BUTTON(2): RM = _BUTTON(3)
  40.  
  41.     IF leftdown THEN 'if it was down
  42.         IF LM = 0 THEN 'and is now up
  43.             IF TIMER - leftdown < ClickThreshold THEN
  44.                 LeftMouse = 2 'clicked
  45.             ELSE 'if it's still down
  46.                 LeftMouse = 0 'the mouse was just released
  47.             END IF
  48.             leftdown = 0 'timer is cleared either way
  49.         ELSE
  50.             LeftMouse = 1 'the left mouse is down , timer should have already been set
  51.         END IF
  52.     ELSE
  53.         IF LM THEN
  54.             leftdown = TIMER 'set the timer to see if we have click or hold events
  55.             LeftMouse = 1 'the left mouse is down
  56.         ELSE
  57.             LeftMouse = 0
  58.         END IF
  59.     END IF
  60.  
  61.     IF middledown THEN 'if it was down
  62.         IF MM = 0 THEN 'and is now up
  63.             IF TIMER - middledown < ClickThreshold THEN
  64.                 MiddleMouse = 2 'clicked
  65.             ELSE 'if it's still down
  66.                 MiddleMouse = 0 'the mouse was just released
  67.             END IF
  68.             middledown = 0 'timer is cleared either way
  69.         ELSE
  70.             MiddleMouse = 1 'the middle mouse is down , timer should have already been set
  71.         END IF
  72.     ELSE
  73.         IF MM THEN
  74.             middledown = TIMER 'set the timer to see if we have click or hold events
  75.             MiddleMouse = 1 'the middle mouse is down
  76.         ELSE
  77.             MiddleMouse = 0
  78.         END IF
  79.     END IF
  80.  
  81.     IF rightdown THEN 'if it was down
  82.         IF RM = 0 THEN 'and is now up
  83.             IF TIMER - rightdown < ClickThreshold THEN
  84.                 RightMouse = 2 'clicked
  85.             ELSE 'if it's still down
  86.                 RightMouse = 0 'the mouse was just released
  87.             END IF
  88.             rightdown = 0 'timer is cleared either way
  89.         ELSE
  90.             RightMouse = 1 'the right mouse is down , timer should have already been set
  91.         END IF
  92.     ELSE
  93.         IF RM THEN
  94.             rightdown = TIMER 'set the timer to see if we have click or hold events
  95.             RightMouse = 1 'the right mouse is down
  96.         ELSE
  97.             RightMouse = 0
  98.         END IF
  99.     END IF
  100.  
  101.  
  102.  

A simple little set of code which can be easily plugged into any other program to give us a set of working mouse values without any hassles.   ;)

This tracks both "click" events and just "down" (or hold) events for us, as well as giving us x/y coordinates and wheel status.  A value of 1 for LeftMouse, RightMouse, MiddleMouse tells us the button is down; a value of 2 tells us the button was actually clicked with an up, down, then up in time, action. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!