Author Topic: Looking for samples of reading mouse input from console window  (Read 3539 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
There used to be some sample code showing how to read mouse input from a console window. For the life of me I cannot find this. can anyone point me to it?

Use the info in help is just not getting me anywhere. For some reason I simply cannot seem to read any mouse input in QB64 1.4.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #1 on: March 30, 2020, 07:08:49 pm »
Here is a sample showing the difficulties I am having:

With the code below, I have 2 loops. The main loop simply prints an incrementing count at location 1,1 so that I can visibly see that this loop is working.

The inner loop should get triggered when mouse input to the console window occurs. Like the outer loop, a count gets incremented and the value would be displayed at location 2,1 so that I can visually see that this inner loop is working. The various mouse values (_MOUSEX, _MOUSEY, _MOUSEBUTTON(1), and _MOUSEWHEEL) would then be displayed at location 3,1.

When I execute the code, the first loop is working, but when I click the mouse button in the console window, all execution seems to stop and the location where I click the mouse simply displays a solid white block just like when you are selecting text with a mouse.

Can anyone tell me what I am doing wrong here?

   
Code: QB64: [Select]
  1. WIDTH 120, 30
  2.  
  3.     _LIMIT 50
  4.     x = x + 1
  5.     LOCATE 1, 1: PRINT "Iterations through main loop:"; x
  6.  
  7.         y = y + 1
  8.         LOCATE 2, 1: PRINT "Iterations through Mouse Input loop:"; y
  9.         LOCATE 3, 1: PRINT "Mouse inputs:"; _MOUSEX, _MOUSEY, _MOUSEBUTTON(1), _MOUSEWHEEL
  10.     LOOP
  11.  
  12.    

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Looking for samples of reading mouse input from console window
« Reply #2 on: March 30, 2020, 07:23:18 pm »
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE

PRINT "Press any key, and I'll give you the scan code for it.  <ESC> quits the demo."
PRINT
PRINT
DO
    x = _CONSOLEINPUT
    IF x = 1 THEN 'read only keyboard input ( = 1)
        c = _CINP
        PRINT c;
    END IF
LOOP UNTIL c = 1
END

I'm on my iPad while my PC is out of commission, so the above is a simple copy-paste from the wiki for individual key presses in the console.

Mouse button input works the same way, except_CONSOLEINPUT would return a value of 2, which we'd store in X.  Change the above to "IF X = 2 THEN" and you should be able to get the data you're looking for, which would be returned by _CINP and stored in variable c.
« Last Edit: March 30, 2020, 07:25:48 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #3 on: March 30, 2020, 07:49:29 pm »
Thanks for the information, Steve. Unfortunately, that still does not work even with the changes you suggested. For some reason, clicking the mouse simply seems to try selecting text in the console window but has no effect on the program.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #4 on: March 30, 2020, 08:42:05 pm »
I tried to simplify this as much as possible and came up with this:

   
Code: QB64: [Select]

When run, the value of _CONSOLEINPUT will be 0 or 1, never 2, even when when clicking the mouse.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #5 on: March 30, 2020, 09:23:10 pm »
More Information:

I found a sample program that was posted prior to the release of QB64 1.4 to demonstrate various console enhancements. I notice that the _CONSOLEFONT is also not working for me.

In an attempt to determine if the problem is some sort of setting on my computer, I also tried to run the sample code on a VM where I have the same problems.

Details:

Windows 10, 1909, with March 2020 updates installed.

Sample code:

Code: QB64: [Select]
  1.  
  2. PRINT "Welcome to a demostration of the Windows Console System for QB64,"
  3. PRINT "as overhauled by Steve!"
  4. PRINT "First, allow me to illustrate two newly implemented commands: SLEEP and CLS"
  5. Pause
  6. PRINT "And now, I'll illustrate how to change colors in the console:"
  7. FOR i = 0 TO 15
  8.     COLOR i, 15 - i
  9.     PRINT "COLORS:"; i; "ON"; 15 - i
  10. Pause
  11. PRINT "And now, we're going to have fun with the console size."
  12. PRINT "Our original screen should be set at 80x25 for default."
  13. PRINT "Press <ANY KEY> when ready to change modes."
  14. Pause
  15. WIDTH 120, 50
  16. PRINT "Here we're set at 120x50."
  17. Pause
  18. WIDTH 120, 50, 120, 50
  19. PRINT "And here we no longer have a scroll buffer; it's also 120x50."
  20. PRINT "Notice the vertical scroll bar is gone?"
  21. Pause
  22. WIDTH 120, 50, 300, 300
  23. PRINT "And here we now have both a vertical and a hortizontal scroll bar."
  24. FOR i = 1 TO 50
  25.     PRINT "See? ";
  26.     _LIMIT 10
  27. PRINT "Be certain to scroll the bar so you can 'See' all the text."
  28. Pause
  29. WIDTH , , 120, 50
  30. PRINT "And now, prepare as I do something amazing!"
  31. Pause
  32. PRINT "TaDa!!  Your font is now size 16!"
  33. Pause
  34. PRINT "And just in case yours was already size 16, it's now size 24!"
  35. Pause
  36. Pause
  37. PRINT "And now, let's take a look at... MOUSE SUPPORT!!"
  38. LOCATE 10, 10
  39. PRINT "<<CLICK HERE TO CONTINUE>>"
  40.     x = _CONSOLEINPUT
  41.     IF _MOUSEX >= 10 AND _MOUSEX <= 38 AND _MOUSEY = 10 AND _MOUSEBUTTON(1) THEN EXIT DO
  42. PRINT "WAAAAAAIIIIIIT A MOMENT!!"
  43. FOR i = 1 TO 40
  44.     PRINT MID$("THE CONSOLE NOW HAS MOUSE SUPPORT?!!", i, 1);
  45.     _DELAY .1
  46. PRINT "WOOOOOOOO!!!!!"
  47. Pause
  48. PRINT "And, of course, I can now work with LOCATE..."
  49. LOCATE 10, 10: PRINT "LOCATE 10,10"
  50. LOCATE 20: PRINT "LOCATE 20"
  51. LOCATE , 20: PRINT "LOCATE ,20"
  52. Pause
  53. PRINT "And, of course, I can now get back single character returns..."
  54. PRINT "Press any key, and I'll give you the scan code for it.  <ESC> quits the demo."
  55.     x = _CONSOLEINPUT
  56.     c = _CINP
  57.     PRINT c;
  58. LOOP UNTIL c = 1
  59.  
  60.  
  61. SUB Pause
  62.     PRINT
  63.     PRINT "Press <ANY KEY> to continue."
  64.     SLEEP
  65.  
Code: [Select]

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #6 on: March 30, 2020, 10:37:58 pm »
Sorry for so many responses, but I finally figured out a partial fix. For the mouse input issues, you have to right click on the left corner of the title bar, select properties, then the Options tab, and deselect "QuickEdit Mode".

This still does not fix the font issues, but does resolve the issue of mouse input not being read from the console.



Marked as best answer by hanness on March 31, 2020, 02:09:42 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Looking for samples of reading mouse input from console window
« Reply #7 on: March 30, 2020, 10:52:07 pm »
Sorry for so many responses, but I finally figured out a partial fix. For the mouse input issues, you have to right click on the left corner of the title bar, select properties, then the Options tab, and deselect "QuickEdit Mode".

This still does not fix the font issues, but does resolve the issue of mouse input not being read from the console.

Aye.  Quickedit can't be enabled for the mouse to work properly.

For the font issues, you'd need to go into the internal/c folder and edit libqb.cpp.  QB64 currently works on windows systems all the way back to XP, but the library routines which support font changing are Windows Vista and above.  Rather than obsoleting QB64  for windows versions older than Vista, the necessary code was remarked out and the functionality removed.

The proper code is still in libqb for you to change the console font, but you need to unremark it, save the changes, and then purge_libqb.bat.  After that, you can use console font with your programs.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Looking for samples of reading mouse input from console window
« Reply #8 on: March 30, 2020, 10:57:38 pm »
The code in question is this segment:

Code: C++: [Select]
  1.  
  2. void sub__consolefont(qbs* FontName, int FontSize){
  3.     /*
  4.     #ifdef QB64_WINDOWS
  5.     SECURITY_ATTRIBUTES SecAttribs = {sizeof(SECURITY_ATTRIBUTES), 0, 1};
  6.     HANDLE cl_conout = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, & SecAttribs, OPEN_EXISTING, 0, 0);
  7.     static int OneTimePause;
  8.     if (!OneTimePause){ // a slight delay so the console can be properly created and registered with Windows, before we try and change fonts with it.      
  9.         Sleep(500);
  10.         OneTimePause=1; //after the first pause, the console should be created, so we don't need any more delays in the future.
  11.     }
  12.     CONSOLE_FONT_INFOEX info = {0};
  13.     info.cbSize       = sizeof(info);
  14.     info.dwFontSize.Y = FontSize; // leave X as zero
  15.     info.FontWeight   = FW_NORMAL;
  16.     if (FontName->len>0){ //if we don't pass a font name, don't change the existing one.
  17.         const size_t cSize = FontName->len;
  18.         wchar_t* wc = new wchar_t[32];
  19.         mbstowcs (wc, (char *)FontName->chr, cSize);
  20.         wcscpy(info.FaceName, wc);
  21.         delete[] wc;
  22.     }
  23.  
  24.     SetCurrentConsoleFontEx(cl_conout, NULL, &info);
  25.     #endif
  26.     */
  27. }

Remove the group comment lines (the /* and */), and save.
After that, run purge_libqb_only.bat (or a similar named batch file).

Then you should be good to go.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Looking for samples of reading mouse input from console window
« Reply #9 on: March 31, 2020, 06:09:20 am »
Thanks. That helps greatly.