Page 1 of 1

[PB 3.94] Getting the print screen button in PB

Posted: Wed Mar 29, 2006 7:42 am
by theteapot
How can I find out if the user has pressed the print screen button on my screen?

There is no #PB_Key_PrintScreen constant! Is there a numeric value which I can use?

The key does get captured with #PB_Key_All.

Thanks, TheTeapot

Posted: Wed Mar 29, 2006 11:13 am
by Shardik
You can detect the Print Screen button with the value 44 (decimal). I would have thought that the constant #VK_PRINT would be correct, but unfortunately it has the value 42...
Here is a short demo code for PB 3.94 (for PB 4 you have to change the OpenWindow statement):

Code: Select all

OpenWindow(1, 0, 0, 200, 15, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "PrintKey Detection")

Repeat
  Select WaitWindowEvent()
    Case #WM_KEYUP
      If EventwParam() = 44
        MessageRequester("Info", "You have pressed the Print key...")
      EndIf
    Case #PB_Event_CloseWindow
      End
  EndSelect
ForEver

Posted: Wed Mar 29, 2006 1:03 pm
by PB
> I would have thought that the constant #VK_PRINT would be correct

It's #VK_SNAPSHOT.

Posted: Wed Mar 29, 2006 2:04 pm
by Shardik
Thank you, PB. You are right. I must have been blind for overlooking this constant :oops:

theteapot should change the line

Code: Select all

If EventwParam() = 44
against

Code: Select all

If EventwParam() = #VK_SNAPSHOT
in my example.

Posted: Wed Mar 29, 2006 4:48 pm
by USCode
Should the constant #PB_Key_PrintScreen be declared by Fred?
If so, please make the request in the Wishlist forum.
The more we can abstract from the native OS API the better!

Posted: Wed Mar 29, 2006 8:31 pm
by theteapot
Sorry, I may not have made myself clear enough. I wanted to see if the user had pressed that key on a screen.

For example:

Code: Select all

InitSprite()
InitKeyboard()
OpenScreen(1024,768,32,"")
Repeat
  ExamineKeyboard()
  Delay(20)
Until KeyboardReleased(#PB_Key_PrintScreen)  ; This will never evalutate to true, because it doesn't exist!
CloseScreen()

Posted: Thu Mar 30, 2006 9:28 am
by Shardik
Replace #PB_Key_PrintScreen by the decimal value 183 and your code example works as wanted...

Posted: Fri Mar 31, 2006 6:20 am
by theteapot
Thanks!

Where did you find it by the way?

Posted: Fri Mar 31, 2006 8:15 am
by Shardik
Where did you find it by the way?
I modified the demo program from PureBASIC help for the KeyboardInkey() command to display the keyboard scancode for each pressed key in screen mode:

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(1024, 768, 32, "")
  Repeat
    FlipBuffers()
    ClearScreen(0, 0, 0)

    ExamineKeyboard()

    For i = 1 To 255
      If KeyboardReleased(i) = 1
        KeyReleased = i
        Break
      EndIf
    Next i

    If StartDrawing(ScreenOutput())
      DrawingMode(1)
      FrontColor(128, 255, 0)
      Locate(300, 360) : DrawText("Please press a key (Terminate program with <ESC>):")
      If KeyReleased = 0
        Locate(430, 420) : DrawText("Key code:")
      Else
        Locate(430, 420) : DrawText("Key code: " + Str(KeyReleased))
      EndIf
      StopDrawing()
    EndIf
  Until KeyboardPushed(#PB_Key_Escape)
EndIf
To display the scan code for the Print key you perhaps have to keep the Print key pressed a longer time...