Trapping Return - Serious problem ?

Post

Posted
Rating:
#1 (In Topic #636)
Regular
Doctor Watson is in the usergroup ‘Regular’
I need to ‘trap’ the Return, F1 and other function keys, but that doesn’t seem to work. Or I’m doing something wrong and I don’t see it.
What follows here comes straight from the Gambas help that pops up when you’re typing code.
But there may be a surprise …
So I made the following test on an otherwise empty form.
- Put Button1 on a Form
- Added Gambas class file (as copied from Gambas help)

Code (gambas)

  1. PUBLIC SUB Button1_KeyPress()
  2.   IF Key.Alt THEN
  3.     Button1.Text = " True " & CString(Time)
  4.   ELSE
  5.     Button1.Text = " False " & CString(Time)

When I run this, it just seems to ignore the IF condition.
Whatever key I hit – be it a function key, numeric, abc – it jumps to the 'False' text.
But when I hit the Alt key, it STILL reports false. So it seems it doesn’t recognize the Alt key.
I tried this for other function keys – Enter, F1, Esc, … - with the same result.
Tried it with a ValueBox. Same error.

Surprise ! When I change Key.Alt to Key.AltKey, whatever key I hit on my keyboard suddenly returns TRUE !

The on-screen help gives a warning : “Keys may have functions assigned to them by OS or other programs; you could close/… your program/other programs/system.”
So I made sure no other programmes were running. Even re-booted my computer a couple of times to make sure. Still the same result.

Any ideas? This is essential for my programme to function.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Public Sub Form_KeyPress()
  2.     ' KeyPress with Alt Key event will often pass through to this Form event.
  3.  
  4.     Dim altSet As Boolean
  5.  
  6.     Try altSet = Key.Alt
  7.     altSet = IIf( Error , False, altSet)
  8.     If altSet Then
  9.         Debug CString(Time) & " True: Alt key is being held down"
  10.     Else
  11.         Debug CString(Time) & " False: Alt key was not held down"
  12.     Endif
  13.  
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
Doctor Watson is in the usergroup ‘Regular’
 Hi PJBlack
I tried your code - deleted the useless Cstring(Time)
Unfortunately the same strange things occur.
When pressing Alt or any other key, console indicates “Test.Form_KeyPress.22:  False: Alt key was not held down”.
I then changed the code to test other function keys. And here’s the strange part : whichever key I try, the result is always the same. With some keys the result is False, with others it’s True.
Then I noticed something : when changing the Key.[Name] the list of available keynames is shown.
They are preceded by a blue or red dot.
When I try a blue-dotted one, like Key.Alt, Key.Shift, the result is False.
When choosing a red-dotted one, like Key.Return, Key.F1, the result is True.
And, as I said, whichever key on the keyboard I press.
There are even KeyNames that don't seem to exist, such as AltGrKey.
I haven’t the foggiest idea of what those red and blue dots stand for, but could we be looking at a bug?

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’

Doctor Watson said

…Then I noticed something : when changing the Key.[Name] the list of available keynames is shown.
They are preceded by a blue or red dot.
When I try a blue-dotted one, like Key.Alt, Key.Shift, the result is False.
When choosing a red-dotted one, like Key.Return, Key.F1, the result is True.
And, as I said, whichever key on the keyboard I press.
There are even KeyNames that don't seem to exist, such as AltGrKey.
I haven’t the foggiest idea of what those red and blue dots stand for, but could we be looking at a bug?

Take a close look at the red & blue list of options and you will notice that the reds are Constants and the blues are Properties.
Image

(Click to enlarge)


On my UK laptop keyboard the <AltGr> key is on the right-hand side of the spacebar. What kind of keyboard layout do you have?

You cannot use Key.AltKey in your code example because it returns an Integer.

Your basic code moved to Form_KeyPress() seems to work for me:-

Code (gambas)

  1. Public Sub Form_KeyPress()
  2.  
  3.   If Key.Alt Then
  4.     Me.Text = " True " & CString(Time)
  5.   Else
  6.     Me.Text = " False " & CString(Time)

If your code is part of a bigger project, just do a quick test project with the minimum code to see if it works. Step through the code using the <F8> key to make sure it is not failing for some other reason.

You could also use this code to check that keys are returning the correct values:-

Code (gambas)

  1. Public Sub Form_KeyPress()
  2. Dim varKey As Variant
  3.  
  4.   varKey = Key.Code
  5.   Me.Caption = "Last key: " & varKey
  6.  
  7.  

…but take note of the warning message in the .Code help screen:-

Image

(Click to enlarge)


…however, this may still provide some useful diagnostic information.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
maybe this will help you :

Code (gambas)

  1. Public Sub Form_KeyPress()   ' or Button1_KeyPress()
  2.  
  3.  If Key.Code = Key.AltKey Then
  4.     Button1.Text = " True " & CString(Time)
  5.   Else
  6.     Button1.Text = " False " & CString(Time)
  7.   Endif  
  8.   Me.Caption = "Last key: " & Key.Code
  9.  
  10.  End

or this :

Code (gambas)

  1. Public Sub Form_KeyPress()
  2.  
  3.    Select Case Key.Code
  4.      
  5.      Case Key.Return
  6.        Button1.TEXT = "RETURN "
  7.        
  8.      Case Key.AltKey
  9.       Button1.Text = " ALT "
  10.    End Select
  11.    Me.Caption = "Last key: " & Key.Code
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have been trying to work out exactly what you are after without success, however, I notice the line PUBLIC SUB Button1_KeyPress(). If you want the key to be activated by the Return key then set the Button Property Default to True. You can also activate the Button with the Esc key by setting the Button Property Cancel to True.

Another simple way to set a shortcut for the Button is to add an & to the Button Text Property. e.g. Button1.Text = "&Click me!" will underline the 'C' and allow you to use the shortcut Alt+C to activate the key.

If all of us are missing the point can you give us an idea what the program, and you hope to achieve.
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
Doctor Watson is in the usergroup ‘Regular’
 Hurray! Grayghhost4,

That does the trick  indeed. That is, for the ‘red-dotted’ keys in the list, those returning an Integer, as mentioned by Steve. And those are just the ones I need.
How simple can it be, but it still doesn’t explain why some different approaches won’t work.

Steve, the AltGrKey is, just as on your keyboard first right of the space bar. It’s a Logitech and it’s AltGr works perfectly. Just tested it with Grayghhost4’s code and still gives the error : “Unknown symbol ‘AltGrKey ‘in class ‘Key’ in Form1”.
I’m going to do a test run on all function keys to find out if there are any more surprises.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
Doctor Watson is in the usergroup ‘Regular’
 Hi Steve and Cogier
Our posts just have come through together.
I will test what you suggest, but as I just wrote, Grayghhost4’s solution seems to work fine.
The mayor keys I need throughout in my program are the F1, used to enter a help screen or pop-up, and the Return key.
And here’s what perhaps started this all : on my keyboard, the ‘Return’ key is named ‘Enter’.
If I find something else, I’ll let you know.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
on my keyboard, the ‘Return’ key is named ‘Enter’.

The 'Return' key (1) is on the main keyboard and the 'Enter' (2) key is on the numeric keyboard.

<IMG src="https://www.cogier.com/gambas/keyboard.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
AltGrKey does not work. (it broken in gambas)

the right alt key is actually called ISO_Level3_Shift  (the Windows keys are called Super)

It's a pain dealing with it

it's hex value with gtk will be 0xfe03 (as per keysymdef.h)
If Key.Code = &hfe03&

with qt it's integer value is 16781571  (annoyingly a different value than gtk)
If Key.Code = 16781571

and then there's wayland that gambas key.class (and Desktop.SendKeys) do not support well.

Gambas Key.class has limitations with the right AltGr key and also it cannot define between left or right Alt/Shift/Ctrl keys
You can do it by manually checking out the key values
Ie.

Public Sub Form_KeyPress()
  Print Str(Key.Code)
End

Run that and hit the key you want to register and make a note of it's code then you can test for that value.

Key codes are not so well supported as keyboards vary throughout the world and gui toolkits like gtk/qt/wayland/etc all vary the codes they give.
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
Also there is this way. (Using Key["Return"] method not Key.Return constant)

Code (gambas)

  1.  
  2. Select Key.Code
  3.    Case Key["Return"], Key["Enter"]
  4.    Print "Enter or return was hit"
  5.  
  6.   Case Key["F1"]
  7.   Print "F1 was hit"
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
Doctor Watson is in the usergroup ‘Regular’
Good morning - or which part of the day it is where you live.
I have been pretty busy testing your suggestions and when one sort of combines some of them even an amateur like self can find a very satisfying solution.
Keypoint was this little code from Steve :

Code (gambas)

  1. Public Sub Form_KeyPress()
  2. Dim varKey As Variant
  3.   varKey = Key.Code
  4.   Me.Caption = "Last key: " & varKey
  5.  End
And I thought ‘why not using KeyCode to recognise any of the keys on a keyboard?’
So I came up with this:
First, create a form with on it:
- Button1
- Button2
- Label1

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4. Public Sub Form_Open()
  5. Me.caption = "Catch that key !"
  6.  
  7. Public Sub Form_KeyPress()
  8.  
  9.   varKey = Key.Code
  10.   Me.caption = Key.text
  11.   If Button1.HasFocus Then
  12.    Button1.Text = "Last key: " & varKey & "  ASCII = " & Str$(Asc(Key.Text))
  13.    Button2.text = ""
  14.   If Button2.HasFocus Then
  15.    Button2.Text = "Last key: " & varKey & "  ASCII = " & Str$(Asc(Key.Text))
  16.    Button1.text = ""
  17.  
  18.   Select Case varKey ‘some examples
  19.     Case 65470
  20.       Label1.text = "Key pressed : F1"
  21.     Case 65293
  22.       Label1.text = "Key pressed : Return"
  23.     Case 65421
  24.       Label1.text = "Key pressed : Enter"
  25.     Case 65027
  26.       Label1.text = "The infamous AltGr !!"
  27.     Case 65513
  28.       Label1.text = "Finally! The Alt key!"
  29.     Case 65289
  30.       Label1.text = "Tab key pressed – even that one!"
  31.     Case Else
  32.       Label1.text =  "No, try another one"
  33.  
  34.  
This enables me to catch any key at any moment. Well, almost any as you may find out.
I added catching ASCII codes as this might also come in handy.
What’s your verdict?

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Regular
stevedee is in the usergroup ‘Regular’

Doctor Watson said

…Keypoint was this little code from Steve…

…What’s your verdict?

Good morning Doc, I think you may have missed the point that these codes may vary (as Bruce also pointed out) between systems due to use of qt or gtk.

Using the Constant names (as grayghost4  pointed out) is the way to code for your application. My code example was just to test that most keys return a unique value (I don't think my laptop returns a code for <Fn> and I think the "windows key" is hijacked by the operating system).

I hope this helps.
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
stevedee is in the usergroup ‘Regular’
…just to underline the point I made above, on my laptop:-

<F1> returns 16777264
<Return> returns 16777220
<AltGr> returns 16781571
<Alt> returns 16777251

…and so on!
Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’

Doctor Watson said

Good morning - or which part of the day it is where you live.
I have been pretty busy testing your suggestions and when one sort of combines some of them even an amateur like self can find a very satisfying solution.
Keypoint was this little code from Steve :

Code (gambas)

  1. Public Sub Form_KeyPress()
  2. Dim varKey As Variant
  3.   varKey = Key.Code
  4.   Me.Caption = "Last key: " & varKey
  5.  End
And I thought ‘why not using KeyCode to recognise any of the keys on a keyboard?’
So I came up with this:
First, create a form with on it:
- Button1
- Button2
- Label1

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4. Public Sub Form_Open()
  5. Me.caption = "Catch that key !"
  6.  
  7. Public Sub Form_KeyPress()
  8.  
  9.   varKey = Key.Code
  10.   Me.caption = Key.text
  11.   If Button1.HasFocus Then
  12.    Button1.Text = "Last key: " & varKey & "  ASCII = " & Str$(Asc(Key.Text))
  13.    Button2.text = ""
  14.   If Button2.HasFocus Then
  15.    Button2.Text = "Last key: " & varKey & "  ASCII = " & Str$(Asc(Key.Text))
  16.    Button1.text = ""
  17.  
  18.   Select Case varKey ‘some examples
  19.     Case 65470
  20.       Label1.text = "Key pressed : F1"
  21.     Case 65293
  22.       Label1.text = "Key pressed : Return"
  23.     Case 65421
  24.       Label1.text = "Key pressed : Enter"
  25.     Case 65027
  26.       Label1.text = "The infamous AltGr !!"
  27.     Case 65513
  28.       Label1.text = "Finally! The Alt key!"
  29.     Case 65289
  30.       Label1.text = "Tab key pressed – even that one!"
  31.     Case Else
  32.       Label1.text =  "No, try another one"
  33.  
  34.  
This enables me to catch any key at any moment. Well, almost any as you may find out.
I added catching ASCII codes as this might also come in handy.
What’s your verdict?

That will be fine for your own programs that will only run on your computer.
If you want to share the software to others you will find problems just using the code values.

Basically for your own personal programs it fine to do whatever but for migration you need to stick to the gambas methods from this page..
/comp/gb.qt4/key - Gambas Documentation

Note the warnings from that page..
NEVER use the key values directly, as they change between GUI components. Always use these constants!
NEVER use numeric values or the Asc function to test an alphabetic key, use the Key array accessor.

the "Key Array assessor" is the method i mentioned using Key["KeyName"]
you will find the Array assessor works fine for most keys except AltGr and for defining between left or right shift/Ctrl keys.

if you plan to share your software with others i'd advise keeping it simple enough to not need to know AltGr and left or right modifier keys and use the gambas methods.
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
Doctor Watson is in the usergroup ‘Regular’
Well then, I’m somehow back to where it all began.
Using key.code & Key.[names] was something I already had tried, but I didn’t know why it went wrong with certain keys. Now I do.
Too bad that my latest idea can’t be used on every computer, because it really works for any key on my computer.
This issue had become rather theoretical for me – although interesting – because /comp/gb.qt4/key - Gambas Documentation lists every key I will ever need in my programme. Mostly <Return>, <F1>, <F2>, <Esc>.
I don’t need ‘Key.Alt’ when there’s ‘Key.AltKey’ or the infamous ‘AltGrKey’. Nor the Windows key as it already being used by Ubuntu.
So I have changed the code to :
' Gambas class file

Code (gambas)

  1. Public Sub Form_Open()
  2. Me.caption = "Catch that key !"
  3.  
  4. Public Sub Form_KeyPress()
  5.  
  6.   Select Case Key.Code
  7.     Case Key.F1
  8.       Label1.text = "Key pressed : F1"
  9.       Label1.text = "Key pressed : Return"
  10.     Case Key.Enter
  11.       Label1.text = "Key pressed : Enter"
  12.     Case Key.AltKey
  13.       Label1.text = "Finally! The Alt key!"
  14.     Case Key.Tab
  15.       Label1.text = "Tab key pressed"
  16.     Case Else
  17.       Label1.text = "You pressed : " & Key.text & " - ASCII " & Str$(Asc(Key.Text))
  18.  

And this will work on every computer? Well, one who runs Linux that is.

I’ll drink to that. Cheers!

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#17
Guru
BruceSteers is in the usergroup ‘Guru’

Doctor Watson said

Well then, I’m somehow back to where it all began.
Using key.code & Key.[names] was something I already had tried, but I didn’t know why it went wrong with certain keys. Now I do.
Too bad that my latest idea can’t be used on every computer, because it really works for any key on my computer.
This issue had become rather theoretical for me – although interesting – because /comp/gb.qt4/key - Gambas Documentation lists every key I will ever need in my programme. Mostly <Return>, <F1>, <F2>, <Esc>.
I don’t need ‘Key.Alt’ when there’s ‘Key.AltKey’ or the infamous ‘AltGrKey’. Nor the Windows key as it already being used by Ubuntu.
So I have changed the code to :
' Gambas class file

Code (gambas)

  1. Public Sub Form_Open()
  2. Me.caption = "Catch that key !"
  3.  
  4. Public Sub Form_KeyPress()
  5.  
  6.   Select Case Key.Code
  7.     Case Key.F1
  8.       Label1.text = "Key pressed : F1"
  9.       Label1.text = "Key pressed : Return"
  10.     Case Key.Enter
  11.       Label1.text = "Key pressed : Enter"
  12.     Case Key.AltKey
  13.       Label1.text = "Finally! The Alt key!"
  14.     Case Key.Tab
  15.       Label1.text = "Tab key pressed"
  16.     Case Else
  17.       Label1.text = "You pressed : " & Key.text & " - ASCII " & Str$(Asc(Key.Text))
  18.  

And this will work on every computer? Well, one who runs Linux that is.

I’ll drink to that. Cheers!

You got it fella.
Personally i'd use this line to catch enter and return as the same key…

Case Key.Enter, Key.Return


I made a keystroke macro recorder.
It worked well on gtk then looking into using on other systems just started giving me a headache lol.
Online now: No Back to the top
1 guest and 0 members have just viewed this.