ExamineKeyboard(), ExamineMouse() work badly

Just starting out? Need help? Post your questions and find answers here.
rooky06
New User
New User
Posts: 5
Joined: Mon Mar 08, 2021 1:48 pm

ExamineKeyboard(), ExamineMouse() work badly

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1510
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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`.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Mijikai
Addict
Addict
Posts: 1519
Joined: Sun Sep 11, 2016 2:17 pm

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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.
rooky06
New User
New User
Posts: 5
Joined: Mon Mar 08, 2021 1:48 pm

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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 ...
User avatar
Mijikai
Addict
Addict
Posts: 1519
Joined: Sun Sep 11, 2016 2:17 pm

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
NicTheQuick
Addict
Addict
Posts: 1510
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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()
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Mijikai
Addict
Addict
Posts: 1519
Joined: Sun Sep 11, 2016 2:17 pm

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1510
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ExamineKeyboard(), ExamineMouse() work badly

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply