canvas keycode weirdness

Everything else that doesn't fall into one of the other PB categories.
pjsmith67
User
User
Posts: 48
Joined: Thu Apr 26, 2018 3:09 pm

canvas keycode weirdness

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: canvas keycode weirdness

Post 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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
pjsmith67
User
User
Posts: 48
Joined: Thu Apr 26, 2018 3:09 pm

Re: canvas keycode weirdness

Post 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.
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: canvas keycode weirdness

Post 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
Hygge
pjsmith67
User
User
Posts: 48
Joined: Thu Apr 26, 2018 3:09 pm

Re: canvas keycode weirdness

Post 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
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: canvas keycode weirdness

Post 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?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: canvas keycode weirdness

Post 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 ...
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: canvas keycode weirdness

Post 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. 
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: canvas keycode weirdness

Post 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
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply