Canvas ALT modifier is not recognised - Bug?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Canvas ALT modifier is not recognised - Bug?

Post by Joubarbe »

Code: Select all

OpenWindow(0, 0, 0, 50, 50, "")
CanvasGadget(0, 0, 0, 50, 50, #PB_Canvas_Keyboard)
SetActiveGadget(0)

Procedure OnKeyUp()
  Debug GetGadgetAttribute(0, #PB_Canvas_Key)
  Debug GetGadgetAttribute(0, #PB_Canvas_Modifiers)
EndProcedure

BindGadgetEvent(0, @OnKeyUp(), #PB_EventType_KeyUp)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Shift and Control (and AltGr) work OK, but not Alt. Am I missing something?

Documentation:
#PB_Canvas_Alt : The 'alt' key is currently pressed.
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Canvas ALT modifier is not recognised - Bug?

Post by Fred »

It works here, it display '2' when alt + another key is pressed (Win10)
Joubarbe
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Canvas ALT modifier is not recognised - Bug?

Post by Joubarbe »

Hmm, interesting. I'm on Win11. There's a typical Windows error sound when I combine Alt and another key. (always shows 0)
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Canvas ALT modifier is not recognised - Bug?

Post by wombats »

I'm on Windows 11. I hear the Windows error sound and see 2 when Alt and another key are pressed and 0 when just Alt is pressed.
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Canvas ALT modifier is not recognised - Bug?

Post by User_Russian »

Left Alt doesn't work. Right Alt works, but GetGadgetAttribute(0, #PB_Canvas_Modifiers) returns 6, not 2.
Joubarbe
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Canvas ALT modifier is not recognised - Bug?

Post by Joubarbe »

Well, I would consider this a bug. Please move this thread to the bug section if you find it appropriate.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Canvas ALT modifier is not recognised - Bug?

Post by jacdelad »

User_Russian wrote: Fri May 30, 2025 9:52 pm Left Alt doesn't work. Right Alt works, but GetGadgetAttribute(0, #PB_Canvas_Modifiers) returns 6, not 2.
Aren't left and right Alt two different keys? Alt and AltGr??
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
Joubarbe
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Canvas ALT modifier is not recognised - Bug?

Post by Joubarbe »

jacdelad wrote: Sat May 31, 2025 7:10 am
User_Russian wrote: Fri May 30, 2025 9:52 pm Left Alt doesn't work. Right Alt works, but GetGadgetAttribute(0, #PB_Canvas_Modifiers) returns 6, not 2.
Aren't left and right Alt two different keys? Alt and AltGr??
They are indeed. I'm talking about Left Alt, the right one also works fine with me. Same as User_Russian.
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Canvas ALT modifier is not recognised - Bug?

Post by HeX0R »

Alt Gr ("right" Alt) = Alt (2) + CTRL (4), therefore = 6
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Canvas ALT modifier is not recognised - Bug?

Post by Fred »

You mean when it's pressed without any other keys ?
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Canvas ALT modifier is not recognised - Bug?

Post by User_Russian »

jacdelad wrote: Sat May 31, 2025 7:10 am
User_Russian wrote: Fri May 30, 2025 9:52 pm Left Alt doesn't work. Right Alt works, but GetGadgetAttribute(0, #PB_Canvas_Modifiers) returns 6, not 2.
Aren't left and right Alt two different keys? Alt and AltGr??
In PB there is only one Alt , not right and left, so they must be the same. https://www.purebasic.com/documentation ... adget.html

Code: Select all

#PB_Canvas_Modifiers 
 Returns the state of the keyboard modifiers for the event. The result is a combination (using bitwise or) of the following values: 
  #PB_Canvas_Shift  : The 'shift' key is currently pressed.
  #PB_Canvas_Alt    : The 'alt' key is currently pressed.
  #PB_Canvas_Control: The 'control' key is currently pressed.
  #PB_Canvas_Command: The 'command' (or "apple") key is currently pressed. (Mac OSX only)
Fred wrote: Sat May 31, 2025 8:51 am You mean when it's pressed without any other keys ?
No, with any other keys. For example, left Alt and space. With PB 6.20 and previous, it doesn't work.
With PB 6.21 beta, it works.
Olli
Addict
Addict
Posts: 1196
Joined: Wed May 27, 2020 12:26 pm

Re: Canvas ALT modifier is not recognised - Bug?

Post by Olli »

(W10
PB 600 LTS x64 demo)

real left alt key : nothing (OS behaviour okay -> alt+F4 alt+space etc...)
real right alt key : modifier code = 6

note :
left and right Control key modifier code = 4
left and right Shift key modifier code = 1
Olli
Addict
Addict
Posts: 1196
Joined: Wed May 27, 2020 12:26 pm

Re: Canvas ALT modifier is not recognised - Bug?

Post by Olli »

I think the left Alt key is reserved for the OS.

With this key, we can :
- switch the menu on or off. (Alt + nothing)
- access to the window properties (Alt+Space)
- quit the window (Alt+F4)
- change the active task (Alt+Tab)
etc...

A way to use more 'normally' the left Alt key is in creating a menu item shortcut, targetting a specific key (ex : Q)

Code: Select all

; (once the window is created)
; (callback procedure is already created and named onKey() )
; (objects identifiers are zeroed)
; (assigning randomly a menu item identifier. Ex : $F001)
CreateMenu(0, WindowID(0) )
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_Q, $F001)
BindMenuEvent(0, $F001, @onKey() )
In this way, the key code and modifier code are 0 (zero). So, a condition can be made to get the menu item code (here EventMenu() = $F001) the shortcut has activated.

The Alt right key provides non zeroed key code and modifer code, allowing the coder to separate a left alt key from a right alt key.
Joubarbe
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Canvas ALT modifier is not recognised - Bug?

Post by Joubarbe »

Fred wrote: Sat May 31, 2025 8:51 am You mean when it's pressed without any other keys ?
Just to be clear, my goal here is to create a hotkey that has ALT as modifier. Example: ALT+E. If I do that, GetGadgetAttribute(0, #PB_Canvas_Modifiers) will return 0. But if I do CTRL+E or SHIFT+E, a proper non-zero modifier code is returned, allowing me to properly detect the hotkey combination.

Besides, ALT is not detected by GetGadgetAttribute(0, #PB_Canvas_Key) when hit independently. SHIFT and CTRL are.

@Olli: Yes I think it's "locked" by the OS indeed. But that would be great to overpass that with the standard functions.
Olli
Addict
Addict
Posts: 1196
Joined: Wed May 27, 2020 12:26 pm

Re: Canvas ALT modifier is not recognised - Bug?

Post by Olli »

Joubarbe wrote: Sat May 31, 2025 3:59 pm Just to be clear, my goal here is to create a hotkey that has ALT as modifier. Example: ALT+E. [...]

@Olli: Yes I think it's "locked" by the OS indeed. But that would be great to overpass that with the standard functions.
This Alt key seems so reserved for the OS for the hotkeys. So, in PureBasic, a menu (invisible) shortcut should do the affair.

Normally, this below is okay :

Code: Select all

Enumeration 1234
#Alt_E
EndEnumeration

OpenWindow(0, 0, 0, 50, 50, "")
CanvasGadget(0, 0, 0, 50, 50, #PB_Canvas_Keyboard)
SetActiveGadget(0)

Procedure OnKeyUp()
  Debug GetGadgetAttribute(0, #PB_Canvas_Key)
  Debug GetGadgetAttribute(0, #PB_Canvas_Modifiers)
EndProcedure

Procedure hotKey()
 Select EventMenu()
  Case #Alt_E
   Debug "Alt+E has been pressed"
 EndSelect
EndProcedure

Define hotKeyMenu = CreateMenu(#PB_Any, WindowID(0) )
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_E, #Alt_E)
BindMenuEvent(hotKeyMenu, #Alt_E, @hotKey() )

BindGadgetEvent(0, @OnKeyUp(), #PB_EventType_KeyUp)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
For low level input, you must use the API functions :
GetKeyboardState() Microsoft documentation
Linux example
Post Reply