International keyboards

Just starting out? Need help? Post your questions and find answers here.
ProphetOfDoom
User
User
Posts: 84
Joined: Mon Jun 30, 2008 4:36 pm
Location: UK

International keyboards

Post 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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: International keyboards

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: International keyboards

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: International keyboards

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: International keyboards

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
ProphetOfDoom
User
User
Posts: 84
Joined: Mon Jun 30, 2008 4:36 pm
Location: UK

Re: International keyboards

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: International keyboards

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
ProphetOfDoom
User
User
Posts: 84
Joined: Mon Jun 30, 2008 4:36 pm
Location: UK

Re: International keyboards

Post 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?
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: International keyboards

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: International keyboards

Post 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?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: International keyboards

Post by juergenkulow »

Hello ProphetOfDoom,

here is an example of an international keyboard, Gurmukhi for Panjabi:
Image
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

Re: International keyboards

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: International keyboards

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: International keyboards

Post by ChrisR »

Why not simply use something like the example in the KeyboardInkey() help
With: If FindString("^", result$)
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: International keyboards

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply