Author Topic: More console mouse input questions  (Read 2708 times)

0 Members and 1 Guest are viewing this topic.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
More console mouse input questions
« on: March 31, 2020, 08:43:27 am »
I have some more questions about mouse and keyboard input in a console.

First, to see if there is any input available from the console, I call the _CONSOLEINPUT function and check to see if the value is something other than 0.

The first problem that I run into is that if the value IS 0, then code execution seems to stop. Take for example this code:

   
Code: QB64: [Select]
  1.  
  2.  
  3.     LoopCounter = LoopCounter + 1
  4.     LOCATE 1, 1: PRINT "Current loop:"; LoopCounter;
  5.  
  6.     x = _CONSOLEINPUT
  7.  
  8.     IF x = 2 THEN
  9.         DO
  10.             z = _MOUSEINPUT
  11.             LOCATE 3, 1: PRINT "Mouse x Position:"; _MOUSEX;
  12.             LOCATE 4, 1: PRINT "Mouse y Position:"; _MOUSEY;
  13.             LOCATE 5, 1: PRINT "Mouse Button 1  :"; _MOUSEBUTTON(1);
  14.             LOCATE 6, 1: PRINT "Mouse Wheel     :"; _MOUSEWHEEL;
  15.         LOOP UNTIL z = 0
  16.  
  17.     END IF
  18.  
  19.  
  20.    

I would expect the first DO LOOP to run constantly, incrementing LoopCounter each time and printing the value. That does not happen. For some unknown reason, LoopCounter increments to 2 and then stops until I either press keys or perform a mouse action within the console window. As soon as I stop pressing keys or performing mouse actions, the code once again stops executing (when _CONSOLEINPUT would return 0).

There are a couple of implications that this has:

First, when you call the _CONSOLEINPUT, the program will freeze until a console input is triggered.
Second, this makes it impossible to accuratly perform checks on the mouse wheel. You see, when the wheel is rotated in one direction, _MOUSEWHEEL returns a 1. Rotated in the other direction it returns a -1. When you stop rotating the wheel it is supposed to return a 0, but that will not happen due to the freezing. It will only change to a 0 once another console event is triggered (mouse action or keyboard).

Here is an even more simplified program. When run, the loopcounter immediately advances to 2, then execution stops and only resumes when keyboard or mouse actions are performed in the console window.

   
Code: QB64: [Select]
  1.  
  2.  
  3.     LoopCounter = LoopCounter + 1
  4.     LOCATE 1, 1: PRINT "Current loop:"; LoopCounter;
  5.  
  6.     x = _CONSOLEINPUT
  7.    

If code execution halts when _CONSOLEINPUT is called, how can I check for input without freezing execution of the program? Am I missing something obvious here or misunderstanding how this is supposed to work?

Marked as best answer by hanness on March 31, 2020, 08:35:18 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: More console mouse input questions
« Reply #1 on: March 31, 2020, 10:50:04 am »
Console input is different than window input.  Instead of acting like an INKEY$ statement, it performs more like an INPUT$(1) command. 

As you've noticed, any call to  _CONSOLEINPUT pauses program execution awaiting user interaction.  It's just the way the command works, just like how INPUT will always halt program execution until <ENTER> is hit.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!