Strange behaviour of Alt Gr key

Discussions related to network, internet and socket programming; also to serial, parallel, Bluetooth, USB etc.
Hated Moron

Strange behaviour of Alt Gr key

Post by Hated Moron »

Apparently this has been the case ever since the Alt Gr key appeared on PC keyboards (to the right of the space bar) but I never knew it until very recently: If you press the Alt Gr key the left-hand Ctrl key will also appear to be pressed!

You can try it for yourself by running the program below (compatible with both BB4W and BBCSDL), or you can run it in a suitable browser here (sorry, not IE or any iOS browsers).

Even more strange is that (on this laptop at least) if you press and hold down the right-hand Ctrl key before pressing the Alr Gr key, then the left Crtl key will then not report being pressed! So it depends on the order in which the keys are pressed.

To avoid this behaviour upsetting your own programs you can usefully test for a Ctrl key being pressed as follows:

Code: Select all

      IF INKEY(-2) AND NOT INKEY(-9) THEN ...
You really do learn something new every day!

Code: Select all

      VDU 23,22,800;300;8,16,16,0
      VDU 23,128,8,16,32,127,32,16,8,0
      VDU 23,129,0,16,56,&54,&92,16,16,0
      VDU 23,130,0,16,16,&92,&54,56,16,0
      VDU 23,131,16,8,4,254,4,8,16,0

      REM Global arrays:
      DIM Legend$(4,13), InKey%(4,13), Width(4,13), Height(4,13)

      REM Legends:
      Legend$() = "`","1","2","3","4","5","6","7","8","9","0","-","=","Backspace", \
      \           "Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","Enter", \
      \           "Caps","A","S","D","F","G","H","J","K","L",";","'","#","", \
      \           "Shift","\","Z","X","C","V","B","N","M",",",".","/","Shift","", \
      \           "Ctrl","Alt"," Space","AltGr","Ctrl",CHR$128,CHR$129,CHR$130,CHR$131

      REM Negative INKEY codes:
      InKey%() = 46,49,50,18,19,20,53,37,22,39,40,24,94,48, \
      \          97,17,34,35,52,36,69,54,38,55,56,57,89,74, \
      \          65,66,82,51,68,84,85,70,71,87,88,80,91,74, \
      \          4,121,98,67,83,100,101,86,102,103,104,105,7,7, \
      \          5,6,99,9,8,26,58,42,122

      REM Relative widths:
      Width() = 1,1,1,1,1,1,1,1,1,1,1,1,1,2.0, \
      \         1.5,1,1,1,1,1,1,1,1,1,1,1.15,1.15,1.2, \
      \         1.8,1,1,1,1,1,1,1,1,1,1,1,1,0, \
      \         1.3,1,1,1,1,1,1,1,1,1,1,1,2.7,0, \
      \         1.0,1.0,7.0,1.0,1.0,1,1,1,1

      REM Relative heights:
      Height() = 0.9 : Height(1,13) = 1.9

      SIZE = 100

      VDU 5
      REPEAT
        y = 550
        FOR r% = 0 TO DIM(Legend$(),1)
          x = 50
          FOR c% = 0 TO DIM(Legend$(),2)
            IF Width(r%,c%) <> 0 THEN
              w = Width(r%,c%) * SIZE - 10 : h = Height(r%,c%) * SIZE
              IF INKEY(-InKey%(r%,c%)) GCOL 2,14 ELSE GCOL 1,5
              RECTANGLE FILL x, y, w, -h
              W% = LEN(Legend$(r%,c%)) * 16
              MOVE x + (w - W%) DIV 2, y - SIZE DIV 2 + @char.y%
              GCOL 15 : PRINT Legend$(r%,c%);
              x += Width(r%,c%) * SIZE
            ENDIF
          NEXT
          y -= SIZE
        NEXT r%
        delay% = INKEY(10)
      UNTIL FALSE
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Strange behaviour of Alt Gr key

Post by Ric »

I have to admit, that's a new one to me too. Fortunately I have no programs that test for alt gr.

Kind regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Hated Moron

Re: Strange behaviour of Alt Gr key

Post by Hated Moron »

Ric wrote: Sat 26 Aug 2023, 22:48 I have to admit, that's a new one to me too. Fortunately I have no programs that test for alt gr.
The problem is not so much programs that test the Alt Gr key (very few do) but programs which test the Ctrl key, which is far more common. If your program tests the Ctrl key, using INKEY(-2) or INKEY(-5), and the user of that program wants to type an accented character (e.g. áéíóú) on a standard keyboard, he will most likely do so by using Alt Gr. Your program will then see the Ctrl key as being pressed when it isn't. :shock:

In my recent testing of keyboard entry of accented characters, I have discovered that BBCEdit (Andy Parkes' IDE, which is supplied alongside SDLIDE) does not accept them at all if entered using Alt Gr! I wouldn't be at all surprised if this issue is the reason, it may be that he tests for Ctrl being pressed in order to implement certain keyboard shortcuts, and this prevents entry of accented characters with Alt Gr.

I have tried to contact Andy about this, but have had no response. Does anybody happen to know if he is still around?
Hated Moron

Re: Strange behaviour of Alt Gr key

Post by Hated Moron »

On 27/08/2023 14:51, J.G.Harston wrote (cross-posted from the Discussion Group):
it appears it's to allow a keyboard with only one ALT key to simulate a RightAlt by pressing Ctrl-Alt.
I don't see how one follows from the other. It's perfectly possible for Ctrl+Alt to have the same effect as Alt Gr, without the latter making the Ctrl key also appear to be pressed.

After all, pressing Alt Gr does not make the left Alt key appear to be pressed, only the Ctrl key. So it's not as though it's been done so they look the same to an application program, they don't.

Similarly, whilst pressing Ctrl+Alt+E does indeed have the same effect as pressing Alt Gr+E (they both generate e-acute) pressing Ctrl+Alt doesn't simulate pressing Alt Gr.

So I'm not convinced this is the explanation.