Page 1 of 1

canvas keycode weirdness

Posted: Sun Jun 08, 2025 9:52 pm
by pjsmith67
Before I declare this a bug, can I get a few people to try this code snippet and see if you get the same result when you press "." and ","? On my system, MacOS 14.7.5, I get 44 for both of them which is ",".

Please tell me I'm not crazy. :-D

Phil

Code: Select all

OpenWindow(0,10,10,800,600,"")
CanvasGadget(1,0,0,800,600,#PB_Canvas_Keyboard)
While WaitWindowEvent()<>#PB_Event_CloseWindow
	If EventType()=#PB_EventType_KeyDown
		Debug "canvas key="+Str(GetGadgetAttribute(1,#PB_Canvas_Key))
	EndIf
Wend

Re: canvas keycode weirdness

Posted: Sun Jun 08, 2025 11:12 pm
by jacdelad
Thanks for not blindly creating a bug report. Quick question: what are your language settings and do you use the ","-key on the number pad?

Re: canvas keycode weirdness

Posted: Sun Jun 08, 2025 11:31 pm
by pjsmith67
Yeah, I want to make sure its not my system that's the problem. This is on my laptop, so no keypad.

Verified language settings are English both on the system and in Purebasic.

Just tried it on my windows 10 box, works fine there.

Re: canvas keycode weirdness

Posted: Mon Jun 09, 2025 12:02 am
by Kiffi
pjsmith67 wrote: Sun Jun 08, 2025 9:52 pmOn my system, MacOS 14.7.5, I get 44 for both of them which is ",".
same on my Mac Mini M4 Sequoia 15.5

This is strange and should be investigated. Until then, you can do the following:
PB-Help wrote:To get text input for the gadget, it is better to watch for #PB_EventType_Input events and use the #PB_Canvas_Input attribute

Code: Select all

OpenWindow(0,10,10,800,600,"")
CanvasGadget(1,0,0,800,600,#PB_Canvas_Keyboard)
While WaitWindowEvent()<>#PB_Event_CloseWindow
  
  Select EventType()
    Case #PB_EventType_Input 
      Debug "canvas key="+Str(GetGadgetAttribute(1,#PB_Canvas_Input)) 
  EndSelect
  
Wend

Re: canvas keycode weirdness

Posted: Mon Jun 09, 2025 12:34 am
by pjsmith67
Thank you for confirming I am not crazy! :-)

I will post a bug report on the Mac bug forum.

Thanks for the suggestion for using input. That works. Did not realize that input acts just like keydown.

Phil

Re: canvas keycode weirdness

Posted: Mon Jun 09, 2025 11:36 pm
by Andre
I just found out, that this seems not a MacOS-specific problem.

The following test code is created on Windows 10, and shows the different values returned by the command and used as constant values:

Code: Select all

; Output the constant values for Keypress (used for CanvasGadget below):
Debug "Constant values of #PB_Key_Up/Down/Left/Right:"
Debug #PB_Key_Up    ; will give 200
Debug #PB_Key_Down  ; will give 208
Debug #PB_Key_Left  ; will give 203
Debug #PB_Key_Right ; will give 205

Procedure CheckKeyPress()
  Protected eventtype, key, changed
  Static x = 20, y = 20
  
  eventtype = EventType()
  
  If eventtype = #PB_EventType_KeyDown
    Key = GetGadgetAttribute(0, #PB_Canvas_Key)
    Debug "Keypress = " + Key   ; FIXME: the cursor keys will give 38 (up), 37 (left), 39 (right), 40 (down), so the #PB_Key_Up/Down/Left/Right constants can't be used!
    Select Key 
      Case 38   ; don't work: #PB_Key_Up
        y - 1
        changed = #True
      Case 40   ; don't work: #PB_Key_Down
        y + 1
        changed = #True
      Case 37   ; don't work: #PB_Key_Left
        x - 1
        changed = #True
      Case 39   ; don't work: #PB_Key_Right
        x + 1
        changed = #True
    EndSelect
  EndIf
  
  If changed = #True
    Debug "New values: x = " + x + " / y = " + y    
  EndIf
EndProcedure


OpenWindow(0, 50, 50, 400, 300, "Canvas test window - press cursor keys!")
CanvasGadget(0, 5, 5, 390, 290, #PB_Canvas_Keyboard)

BindGadgetEvent(0, @CheckKeyPress(), #PB_EventType_KeyDown)

SetActiveGadget(0)

Repeat
  event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
Don't know, if this also qualifies as a bug?

Re: canvas keycode weirdness

Posted: Tue Jun 10, 2025 3:03 am
by idle
Andre wrote: Mon Jun 09, 2025 11:36 pm I just found out, that this seems not a MacOS-specific problem.

The following test code is created on Windows 10, and shows the different values returned by the command and used as constant values:

Code: Select all

; Output the constant values for Keypress (used for CanvasGadget below):
Debug "Constant values of #PB_Key_Up/Down/Left/Right:"
Debug #PB_Key_Up    ; will give 200
Debug #PB_Key_Down  ; will give 208
Debug #PB_Key_Left  ; will give 203
Debug #PB_Key_Right ; will give 205

Procedure CheckKeyPress()
  Protected eventtype, key, changed
  Static x = 20, y = 20
  
  eventtype = EventType()
  
  If eventtype = #PB_EventType_KeyDown
    Key = GetGadgetAttribute(0, #PB_Canvas_Key)
    Debug "Keypress = " + Key   ; FIXME: the cursor keys will give 38 (up), 37 (left), 39 (right), 40 (down), so the #PB_Key_Up/Down/Left/Right constants can't be used!
    Select Key 
      Case 38   ; don't work: #PB_Key_Up
        y - 1
        changed = #True
      Case 40   ; don't work: #PB_Key_Down
        y + 1
        changed = #True
      Case 37   ; don't work: #PB_Key_Left
        x - 1
        changed = #True
      Case 39   ; don't work: #PB_Key_Right
        x + 1
        changed = #True
    EndSelect
  EndIf
  
  If changed = #True
    Debug "New values: x = " + x + " / y = " + y    
  EndIf
EndProcedure


OpenWindow(0, 50, 50, 400, 300, "Canvas test window - press cursor keys!")
CanvasGadget(0, 5, 5, 390, 290, #PB_Canvas_Keyboard)

BindGadgetEvent(0, @CheckKeyPress(), #PB_EventType_KeyDown)

SetActiveGadget(0)

Repeat
  event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
Don't know, if this also qualifies as a bug?
They appear to be for the screen. The arrow keys are returning the VK_Codes , #VK_UP ...

Re: canvas keycode weirdness

Posted: Tue Jun 10, 2025 5:19 am
by Demivec
idle wrote: Tue Jun 10, 2025 3:03 am
Andre wrote: Mon Jun 09, 2025 11:36 pm Don't know, if this also qualifies as a bug?
They appear to be for the screen. The arrow keys are returning the VK_Codes , #VK_UP ...

Code: Select all

Key = GetGadgetAttribute(0, #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. 

Re: canvas keycode weirdness

Posted: Tue Jun 10, 2025 8:50 pm
by Andre
Demivec wrote: Tue Jun 10, 2025 5:19 am
idle wrote: Tue Jun 10, 2025 3:03 am
Andre wrote: Mon Jun 09, 2025 11:36 pm Don't know, if this also qualifies as a bug?
They appear to be for the screen. The arrow keys are returning the VK_Codes , #VK_UP ...

Code: Select all

Key = GetGadgetAttribute(0, #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. 
Oh well... :oops: should better learn the help content, instead of only translating it :lol:
Thank you both for your help!

So the shortened and working example for using the cursor keys is looking like this:

Code: Select all

Procedure CheckKeyPress()
  Protected eventtype, key, changed
  Static x = 20, y = 20
  
  eventtype = EventType()
  
  If eventtype = #PB_EventType_KeyDown
    Key = GetGadgetAttribute(0, #PB_Canvas_Key)
    Select Key 
      Case #PB_Shortcut_Up
        y - 1
        changed = #True
      Case #PB_Shortcut_Down
        y + 1
        changed = #True
      Case #PB_Shortcut_Left
        x - 1
        changed = #True
      Case #PB_Shortcut_Right
        x + 1
        changed = #True
    EndSelect
  EndIf
  
  If changed = #True
    Debug "New values: x = " + x + " / y = " + y    
  EndIf
EndProcedure


If OpenWindow(0, 50, 50, 400, 300, "Canvas test window - press cursor keys!")
  CanvasGadget(0, 5, 5, 390, 290, #PB_Canvas_Keyboard)
  
  BindGadgetEvent(0, @CheckKeyPress(), #PB_EventType_KeyDown)
  
  SetActiveGadget(0)
  
  Repeat
    event = WaitWindowEvent()
  Until event = #PB_Event_CloseWindow
EndIf