Page 1 of 2

International keyboards

Posted: Fri Jan 29, 2021 11:48 am
by ProphetOfDoom
Hi there,
Is it okay to assume that the caret symbol (^) is obtained by typing SHIFT+6 on any keyboard? And the plus character with SHIFT+=? In general, what assumptions can I make about obtaining ascii symbols that won’t break my code on a foreign keyboard?
Thank you.

Re: International keyboards

Posted: Fri Jan 29, 2021 1:40 pm
by Tenaja
Some nordic countries place the ampersand on 6, instead of the carat.

It should still work, as long as it's not used for fast keys like a game; they place the carat on the far right, down a row.

Re: International keyboards

Posted: Fri Jan 29, 2021 2:09 pm
by TI-994A
ProphetOfDoom wrote:...Is it okay to assume that the caret symbol (^) is obtained by typing SHIFT+6 on any keyboard?...
Generally, it's not good practice to determine key presses by keyboard layout. Input should be strictly ascertained by their ASCII codes.

Re: International keyboards

Posted: Fri Jan 29, 2021 2:23 pm
by Marc56us
ProphetOfDoom wrote: Is it okay to assume that the caret symbol (^) is obtained by typing SHIFT+6 on any keyboard? And the plus character with SHIFT+=?
No, it is not. With french keyboard:
^ = Alt Gr + 9
+ = Maj + +

Like TI-994A said: it is preferable to use ascii codes.
But except for games and simulators where the position of the keys is preferable for ergonomics,

For keyboard layout by country, search for
keyboard layout by country

PS. If you knew how annoying our french keyboards are for system administrators :x
\ = Alt Gr + 8 (this is why we prefer linux console with / who is at bottom left) :mrgreen:
@ = Alt Gr + 0
etc

Re: International keyboards

Posted: Fri Jan 29, 2021 3:17 pm
by TI-994A
Marc56us wrote:...But except for games and simulators where the position of the keys is preferable for ergonomics...
Even that could prove problematic. A simple example would be the long-standing issue of the WASD keys, which provide a left-handed ergonomic alternative to the arrow keys. On the popular DVORAK keyboards, the positions of the W, S and D keys vary from that on QWERTY keyboards, essentially rendering the option impractical.

For such mechanical purposes, a good practice would be to facilitate customised key mappings.

Re: International keyboards

Posted: Fri Jan 29, 2021 3:26 pm
by ProphetOfDoom
Wow thanks for the quick replies.
This is what I feared.
Let me explain my use case...
I'm making a scientific calculator. I thought it would be cool to draw it on a WindowedScreen, with artwork to make it look like a real hardware calculator. But there doesn't seem to be a way to get ascii values for + * ^ operators when the user types them? KeyboardPushed() and the keyboard shortcuts API seem to be tied to scancodes and a very limited set of keys. Maybe I will be forced to just use the boring grey gadgets.

Re: International keyboards

Posted: Fri Jan 29, 2021 3:49 pm
by IdeasVacuum
...not at all, you can use the Unicode erm, code, for any character not found in ASCII. Just ensure that you have a good font such as "Arial Unicode MS" that contains the required characters. You can ship fonts with your software if necessary.

Re: International keyboards

Posted: Fri Jan 29, 2021 4:58 pm
by ProphetOfDoom
Sorry not understanding this IdeasVacuum.
Marc56us says his caret on his French keyboard is ALT-GR+9; on my English keyboard it’s SHIFT+6. So which PureBasic API function can report the caret character, rather than the keys the user pressed, which could mean either?

Re: International keyboards

Posted: Fri Jan 29, 2021 4:59 pm
by TI-994A
ProphetOfDoom wrote:...making a scientific calculator. I thought it would be cool to draw it on a WindowedScreen, with artwork to make it look like a real hardware calculator. But there doesn't seem to be a way to get ascii values for + * ^ operators when the user types them?
Here's one approach:

Code: Select all

Enumeration
  #MainWindow
  #Canvas
  #Sprite
  #Arial
EndEnumeration

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered

If InitSprite() And
   OpenWindow(#MainWindow, 0, 0, 300, 400, "Canvas Input", wFlags) And 
   OpenWindowedScreen(WindowID(#MainWindow), 10, 10, 280, 380)
 
  LoadFont(#Arial, "Arial", 36, #PB_Font_Bold)
  CanvasGadget(#Canvas, 290, 0, 10, 400, #PB_Canvas_Keyboard)
  SetWindowColor(#MainWindow, #White)
  SetActiveGadget(#Canvas)
  ClearScreen(#Cyan)
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        appQuit = #True     
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Canvas
            Select EventType()
              Case #PB_EventType_Input
                ClearScreen(#Cyan)
                input$ = Chr(GetGadgetAttribute(#Canvas, #PB_Canvas_Input))
                If StartDrawing(ScreenOutput())                       
                    DrawingMode(#PB_2DDrawing_Transparent)
                    DrawingFont(FontID(#Arial))
                    DrawText(125, 125, input$, #Blue)
                  StopDrawing() 
                  FlipBuffers()
                EndIf                   
            EndSelect
        EndSelect
    EndSelect
  Until appQuit 
 
EndIf

Re: International keyboards

Posted: Fri Jan 29, 2021 8:06 pm
by IdeasVacuum
... I thought you meant your Calculator would have it's own keys. If the program has to use keyboard input, how about asking the User, on first use only, to identify the keys?

Re: International keyboards

Posted: Fri Jan 29, 2021 9:35 pm
by juergenkulow
Hello ProphetOfDoom,

here is an example of an international keyboard, Gurmukhi for Panjabi:
Image

Re: International keyboards

Posted: Sat Jan 30, 2021 1:39 am
by BarryG
ProphetOfDoom wrote:which PureBasic API function can report the caret character, rather than the keys the user pressed
This is for Windows only and should report a Shift+Caret keypress for any keyboard. Could some people test and let me know? Thanks!

Code: Select all

; This structure maybe not needed?
; Structure KeyboardState
;   b.b[256]
; EndStructure
; kbs.KeyboardState

key.w=VkKeyScanEx_(Asc("^"),GetKeyboardLayout_(0))
k$=Space(9)
ToAscii_(key,MapVirtualKey_(key,#MAPVK_VK_TO_CHAR),@kbs,@k$,0)
VK_CARET=Asc(Trim(k$))

Macro KeyIsDown(key)
  GetAsyncKeyState_(key) & $8000
EndMacro

Macro KeyIsUp(key)
  GetAsyncKeyState_(key)=0
EndMacro

Debug "Press the Caret key..."

Repeat
  Sleep_(1)
  If KeyIsDown(#VK_SHIFT) And KeyIsDown(VK_CARET) ; Shift+Caret key down.
    Debug "Caret pressed"
    Repeat : Sleep_(1) : Until KeyIsUp(VK_CARET) ; Wait for Caret key up.
  EndIf
ForEver

Re: International keyboards

Posted: Sat Jan 30, 2021 9:44 am
by STARGÅTE
Doesn't work here, with german keyboard. Additionally the ^ character is entered here on a german keyboard without shift.

Debug show, that:
key = 220
VK_CARET = 94
k$ = ^
Image

Re: International keyboards

Posted: Sun Jan 31, 2021 1:57 pm
by ChrisR
Why not simply use something like the example in the KeyboardInkey() help
With: If FindString("^", result$)

Re: International keyboards

Posted: Sun Jan 31, 2021 2:16 pm
by STARGÅTE
I'm not sure if it is a bug, but here on german windows 10, PB 5.72 the result of KeyboardInkey() is "幞" when I press the [^]-key.
In this case FindString("^", result$) wouldn't work :?

Code: Select all

Enumeration
	#Window
EndEnumeration

InitSprite()
InitKeyboard()

OpenWindow(#Window, 100, 100, 800, 450, "KeyboardInkey", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Window), 0, 0, 800, 450)

Define String.s

Repeat
	Repeat
		Select WindowEvent()
			Case #PB_Event_CloseWindow
				Break 2
			Case #PB_Event_None
				Break
		EndSelect
	ForEver
	
	ExamineKeyboard()
	String = KeyboardInkey()
	If String
		Debug String + " (0x"+Hex(Asc(String))+")"
	EndIf
	
	ClearScreen($000000)
	FlipBuffers()
ForEver
Since the hex number is 5E5E it seems like, it is just doubled, because 5E is the ^ character.