Page 1 of 1

ExamineKeyboard(), ExamineMouse() work badly

Posted: Mon Mar 08, 2021 4:45 pm
by rooky06
Hello.

I discover PB and try it before to buy the licence.

So i coded it :

Code: Select all

OpenConsole()
a=InitKeyboard()
b=InitMouse()
Repeat
  ExamineKeyboard()
  key$=KeyboardInkey()
  Print(key$)
 
  ExamineMouse();
  If MouseButton(#PB_MouseButton_Left) <> 0
   Print("click")
  EndIf

  Delay(10)
ForEver
End
I have 2 problems :
1 / The part with ExamineMouse never works.
2 / Sometimes an error appears 'need openscreen before ExamineKeyboard()

I can't find solution because i read the help file and everything seems fine... for the beginner that I am

Thank you for your help

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Mon Mar 08, 2021 4:59 pm
by NicTheQuick
These functions are only valid if used in the context of an open screen. You can not use them for application only code.

Look into the annotations for `ExamineMouse()`. There is a sentence which references `Screen`.

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Mon Mar 08, 2021 5:44 pm
by Mijikai
ExamineKeyboard(), ExamineMouse() work as described by the help!

I assume you want the console cursor pos rather than the mouse pos, yes there is no PB command.
However you can just use the respective OS Api.

Example (Windows only):

Code: Select all

EnableExplicit
        
Procedure.i Main()
  Protected key.s
  Protected csbi.CONSOLE_SCREEN_BUFFER_INFO
  If OpenConsole("Test")
    EnableGraphicalConsole(#True);<- enable console cursor
    Repeat
      key = Inkey();<- check for keypress
      If key
        If key = #CR$
          PrintN("")
        Else
          Print(key)
        EndIf
      EndIf
      Debug GetConsoleScreenBufferInfo_(GetStdHandle_(#STD_OUTPUT_HANDLE),@csbi);<- OS Api to read out the console cursor pos
      Debug csbi\dwCursorPosition\x
      Debug csbi\dwCursorPosition\y
      Delay(1)
    Until RawKey() = #VK_ESCAPE;<- OS key value!
    CloseConsole()
  EndIf
  ProcedureReturn
EndProcedure

Main()

End
The console functions of PB are really good for 99% of what i do
and for the rest i just use the (respective) OS Api.

Im not sure if this works with the demo... iirc there are limitations regarding OS Apis.

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 1:00 am
by rooky06
Thank you, but I may have explained myself incorrectly. I would just like to know when a click occurs anywhere on the screen and not just on my console or window application because I am learning with the help but when I use the code the mouse pointer disappears from the screen ... the buttons work but impossible to see the point of the mouse.
Sorry but I discovered today purebasic ...

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 9:51 am
by Mijikai
Example (Windows only):

Code: Select all

EnableExplicit
       
Procedure.i Main()
  Protected key.s
  Protected cursor.POINT
  If OpenConsole("Test")
    EnableGraphicalConsole(#True);<- enable console cursor
    Repeat
      key = Inkey();<- check for keypress
      If key
        If key = #CR$
          PrintN("")
        Else
          Print(key)
        EndIf
      EndIf
      GetCursorPos_(@cursor);<- OS Api get mouse pos (PBs DesktopMouseX() does the same)
      Debug cursor\x
      Debug cursor\y
      If GetAsyncKeyState_(#VK_LBUTTON) & $8000;<- OS Api check the left mouse button
        Debug "MOUSE PRESSED!"
      EndIf
      Delay(1)
    Until RawKey() = #VK_ESCAPE;<- OS key value!
    CloseConsole()
  EndIf
  ProcedureReturn
EndProcedure

Main()

End
Once again i dont know if this works with the demo version.
The mouse and keyboard functions you mention only work with a screen (2d lib - DX input).
So there is no way those function work outside a screen.
The solution here are OS Api functions or for the mouse location PBs DesktopMouseXY() functions.

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 11:49 am
by TI-994A
rooky06 wrote:...to know when a click occurs anywhere on the screen and not just on my console or window application...
Here's a cross-platform way to obtain the global mouse position:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
OpenWindow(0, 0, 0, 400, 150, "Global Mouse Position", wFlags)
TextGadget(0, 0, 40, 400, 30, "", #PB_Text_Center)
TextGadget(1, 0, 70, 400, 30, "", #PB_Text_Center)

Repeat
  
  Select WindowEvent()   
      
    Case #PB_Event_CloseWindow
      appQuit = #True
      
    Case 0
      SetGadgetText(0, "Desktop mouse position: " +
                       Str(DesktopMouseX()) + "," +
                       Str(DesktopMouseY()))
      
      SetGadgetText(1, "Window mouse position: " +
                       Str(WindowMouseX(0)) + "," +
                       Str(WindowMouseX(0)))
      Delay(20)  
      
  EndSelect
  
Until appQuit

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 12:44 pm
by NicTheQuick
Actually the correct way would be to use a timer and not a delay if the event is 0:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 400, 150, "Global Mouse Position", wFlags)
TextGadget(0, 0, 40, 400, 30, "", #PB_Text_Center)
TextGadget(1, 0, 70, 400, 30, "", #PB_Text_Center)

AddWindowTimer(0, 0, 20)

Repeat
	
	Select WaitWindowEvent()   
			
		Case #PB_Event_CloseWindow
			Break
			
		Case #PB_Event_Timer
			If EventTimer() = 0
				SetGadgetText(0, "Desktop mouse position: " +
				                 Str(DesktopMouseX()) + "," +
				                 Str(DesktopMouseY()))
				
				SetGadgetText(1, "Window mouse position: " +
				                 Str(WindowMouseX(0)) + "," +
				                 Str(WindowMouseX(0)))
			EndIf
			
	EndSelect
	
ForEver

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 2:18 pm
by TI-994A
NicTheQuick wrote:Actually the correct way would be to use a timer and not a delay if the event is 0...
Just an adaptation from the manual, which uses a delay with the WindowEvent() function:

> PureBasic Manual - DesktopMouseX()

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 6:23 pm
by Mijikai
@TI-994A your code is correct as you only pause after the event queue is cleared.
However using a EventTimer makes it more efficient resource wise.

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Tue Mar 09, 2021 6:38 pm
by NicTheQuick
Mijikai wrote:@TI-994A your code is correct as you only pause after the event queue is cleared.
However using a EventTimer makes it more efficient resource wise.
When setting the text of a gadget everytime the event queue is empty there will be new events generated because of that and the whole process can only sleep while the Delay(20) is running. However while this command is sleeping nothing else can happen which is also not that good. :D
Well, I understand this is only a small example and it comes directly from the help, and nobody cares much about it. I just wanted to make some constructive criticism. :wink:

Re: ExamineKeyboard(), ExamineMouse() work badly

Posted: Wed Mar 10, 2021 3:44 am
by TI-994A
NicTheQuick wrote:...I just wanted to make some constructive criticism.
Nevertheless, it is quite unproductive and confusing to suggest alternatives without clarification.

A timer would work more productively, but only if the WaitWindowEvent() function is being used. However, the code sample in question utilises the WindowEvent() function, which requires a delay in its loop. As per the manual.