canvas raw keys: bug or feature? [Linux & Windows]

Post bugreports for the Linux version here
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

canvas raw keys: bug or feature? [Linux & Windows]

Post by BorisTheOld »

We've written a number of GUI classes that use the Canvas gadget to give us access to additional events and text manipulation facilities for Buttons, String gadgets, etc. We're getting around the following problem by preprocessing the raw key values from PB, and converting them into custom values -- a single unique value for each key on the keyboard.

The documentation states:
#PB_Canvas_Key Returns the key that was pressed or released in a #PB_EventType_KeyDown or #PB_EventType_KeyUp event. The returned value is one of the #PB_Shortcut_... values used by the AddKeyboardShortcut() function. This attribute returns raw key presses.
We've found that this is only partly true and is different for Linux and Windows. The following code can be used to show the problems.

Code: Select all

OpenWindow(0,0,0,200,200,"Canvas Gadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(1, 50, 50, 100, 30, #PB_Canvas_Border | #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
SetActiveGadget(1)
Repeat
  iWindowEvent  = WaitWindowEvent()
  Select iWindowEvent
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_KeyUp
          Debug GetGadgetAttribute(1, #PB_Canvas_Key)
;        Case #PB_EventType_KeyDown
;          Debug GetGadgetAttribute(1, #PB_Canvas_Key)
;        Case #PB_EventType_Input
;          Debug Chr(GetGadgetAttribute(1, #PB_Canvas_Input))
      EndSelect
  EndSelect
Until iWindowEvent = #PB_Event_CloseWindow
The following conditions must be met in order to generate the correct "#PB_Shortcut_...." values for letters and keypad keys:

Linux (KeyUp): Caps lock on (or the Shift key pressed), Num lock must be on
Linux (KeyDown): Num lock must be on

Windows (KeyUp): Num lock must be on
Windows (KeyDown): Num lock must be on

Rather than generating raw key codes, PB is generating case sensitive codes for letters and keypad keys. Also, the keys for special characters such as comma, semicolon, etc, and the keypad Enter, have no defined "#PB_Shortcut_...." values.

I'd like to know if this is a bug or is the intended behaviour.

Every key should have only one possible code, and the code should be defined in the list of "#PB_Shortcut_...." values, or perhaps in its own set of "#PB_Rawkey_...." values to avoid creating problems for the shortcut feature.

Thanks
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: canvas raw keys: bug or feature? [Linux & Windows]

Post by #NULL »

I have the same problem.

Code: Select all

OpenWindow(0,0,0,200,200,"Canvas Gadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(1, 50, 50, 100, 30, #PB_Canvas_Border | #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
SetActiveGadget(1)

Debug "value E: " + #PB_Shortcut_E
Debug "value Left: " + #PB_Shortcut_Left
Debug ""

Repeat
  iWindowEvent  = WaitWindowEvent()
  Select iWindowEvent
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_KeyUp
          Debug "up  : " + GetGadgetAttribute(1, #PB_Canvas_Key)
          Select GetGadgetAttribute(1, #PB_Canvas_Key)
            Case #PB_Shortcut_E    : Debug "up  : E"
            Case #PB_Shortcut_Left : Debug "up  : Left"
          EndSelect
       Case #PB_EventType_KeyDown
         Debug "down: " + GetGadgetAttribute(1, #PB_Canvas_Key)
          Select GetGadgetAttribute(1, #PB_Canvas_Key)
            Case #PB_Shortcut_E    : Debug "down: E"
            Case #PB_Shortcut_Left : Debug "down: Left"
          EndSelect
      EndSelect
  EndSelect
Until iWindowEvent = #PB_Event_CloseWindow
'up : Left' is printed but 'up : E' is not, because the value returned by GetGadgetAttribute() is different from #PB_Shortcut_E.
It works in combination with the shift key, and the down event also works ok without shift.
output:

Code: Select all

value E: 69
value Left: 65361

down: 69
down: E
up  : 101
down: 65361
down: Left
up  : 65361
up  : Left
PB 5.61 x64, Ubuntu 16.04
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: canvas raw keys: bug or feature? [Linux & Windows]

Post by BorisTheOld »

@ #NULL

I guess there aren't too many people processing canvas key events - it's been four years since I started this thread. :D

Rod
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: canvas raw keys: bug or feature? [Linux & Windows]

Post by #NULL »

at least not on linux.
i now use the following:

Code: Select all

              Case #PB_EventType_KeyUp
                tmpKeyUpShortcut = GetGadgetAttribute(canvas, #PB_Canvas_Key)
                If tmpKeyUpShortcut >= 97 And tmpKeyUpShortcut <= 122
                  tmpKeyUpShortcut - 32
                EndIf
                Select tmpKeyUpShortcut
                  Case #PB_Shortcut_A     :   ...
                EndSelect
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: canvas raw keys: bug or feature? [Linux & Windows]

Post by SeregaZ »

i try to make some simple game, and a little stuck. how to detect double pressing? left and up arrows for example?

now i get direction from this event selector, but it not detects that "doubled" buttons case

Code: Select all

           Case #MainCanvasWindow
             If EventType() = #PB_EventType_KeyDown
               ButtonCode = GetGadgetAttribute(#MainCanvasWindow, #PB_Canvas_Key)
               Select ButtonCode
                 Case #PB_Shortcut_Left
                   UnitDirection = 1
                   UnitMove = 1

                 Case #PB_Shortcut_Right
                   UnitDirection = 3
                   UnitMove = 1

                 Case #PB_Shortcut_Up
                   UnitDirection = 2
                   UnitMove = 1

                 Case #PB_Shortcut_Down
                   UnitDirection = 4
                   UnitMove = 1
                   
                 Default
                   Debug ButtonCode
                   
               EndSelect
             EndIf
SeregaZ
Enthusiast
Enthusiast
Posts: 617
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: canvas raw keys: bug or feature? [Linux & Windows]

Post by SeregaZ »

wait... i see some small spark in my head :) probably i need trace both case - key press and key relise.



*later

idea was nice, but why windows is loose some keys? i meant for example: i press and hold down, then same pressing - add left. so i hold two buttons, then unhold one of this buttons - and sometimes all is stops. system is didnt see one of holding buttons.

*later
probably is fine :)

Code: Select all

           Case #MainCanvasWindow
             Select EventType() 
               Case #PB_EventType_KeyDown
                 ButtonCode = GetGadgetAttribute(#MainCanvasWindow, #PB_Canvas_Key)
                 Select ButtonCode ;{
                   Case #PB_Shortcut_Left
                     UnitDirection = SetBit(UnitDirection, NumToBit(3))
                     UnitOldDirection = 8
                     
                   Case #PB_Shortcut_Up
                     UnitDirection = SetBit(UnitDirection, NumToBit(2))
                     UnitOldDirection = 4
                     
                   Case #PB_Shortcut_Right
                     ;Debug "r"
                     UnitDirection = SetBit(UnitDirection, NumToBit(1))
                     UnitOldDirection = 2

                   Case #PB_Shortcut_Down
                     UnitDirection = SetBit(UnitDirection, NumToBit(0))
                     UnitOldDirection = 1
                   ;}
                 EndSelect
                 
               Case #PB_EventType_KeyUp
                 ButtonCode = GetGadgetAttribute(#MainCanvasWindow, #PB_Canvas_Key)
                 Select ButtonCode ;{
                   Case #PB_Shortcut_Left
                     UnitDirection = ClearBit(UnitDirection, NumToBit(3))
                   Case #PB_Shortcut_Up
                     UnitDirection = ClearBit(UnitDirection, NumToBit(2))
                   Case #PB_Shortcut_Right
                     UnitDirection = ClearBit(UnitDirection, NumToBit(1))                   
                   Case #PB_Shortcut_Down 
                     UnitDirection = ClearBit(UnitDirection, NumToBit(0))
                     ;}
                 EndSelect   
             EndSelect
Post Reply