Trapping Return - Serious problem ?
Posted
#1
(In Topic #636)
Regular

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)
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.
You eat an elephant one small bite at a time.
Posted
Enthusiast

Code (gambas)
- ' KeyPress with Alt Key event will often pass through to this Form event.
Posted
Regular

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.
You eat an elephant one small bite at a time.
Posted
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.
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:-
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:-
…but take note of the warning message in the .Code help screen:-
…however, this may still provide some useful diagnostic information.
Posted
Enthusiast

or this :
Posted
Guru

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.
Posted
Regular

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.
You eat an elephant one small bite at a time.
Posted
Regular

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.
You eat an elephant one small bite at a time.
Posted
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>
Posted
Guru

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.
Posted
Guru

Posted
Regular

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 :
So I came up with this:
First, create a form with on it:
- Button1
- Button2
- Label1
Code (gambas)
- ' Gambas class file
- Button2.text = ""
- Button1.text = ""
- Case 65470
- Label1.text = "Key pressed : F1"
- Case 65293
- Label1.text = "Key pressed : Return"
- Case 65421
- Label1.text = "Key pressed : Enter"
- Case 65027
- Label1.text = "The infamous AltGr !!"
- Case 65513
- Label1.text = "Finally! The Alt key!"
- Case 65289
- Label1.text = "Tab key pressed – even that one!"
- Label1.text = "No, try another one"
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.
You eat an elephant one small bite at a time.
Posted
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.
Posted
Regular

<F1> returns 16777264
<Return> returns 16777220
<AltGr> returns 16781571
<Alt> returns 16777251
…and so on!
Posted
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 :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
- Label1This enables me to catch any key at any moment. Well, almost any as you may find out.Code (gambas)
' Gambas class file Button2.text = "" Button1.text = "" Case 65470 Label1.text = "Key pressed : F1" Case 65293 Label1.text = "Key pressed : Return" Case 65421 Label1.text = "Key pressed : Enter" Case 65027 Label1.text = "The infamous AltGr !!" Case 65513 Label1.text = "Finally! The Alt key!" Case 65289 Label1.text = "Tab key pressed – even that one!" Label1.text = "No, try another one"
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.
Posted
Regular

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)
- Label1.text = "Key pressed : F1"
- Label1.text = "Key pressed : Return"
- Label1.text = "Key pressed : Enter"
- Label1.text = "Finally! The Alt key!"
- Label1.text = "Tab key pressed"
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.
You eat an elephant one small bite at a time.
Posted
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 fileCode (gambas)
Label1.text = "Key pressed : F1" Label1.text = "Key pressed : Return" Label1.text = "Key pressed : Enter" Label1.text = "Finally! The Alt key!" Label1.text = "Tab key pressed"
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.
1 guest and 0 members have just viewed this.




