Select Key.Code
Case Key["Return"], Key["Enter"]
Print "Enter or return was hit"
Case Key["F1"]
Print "F1 was hit"
End Select
Trapping Return - Serious problem ?
- BruceSteers
- Posts: 1574
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Trapping Return - Serious problem ?
Also there is this way. (Using Key["Return"] method not Key.Return constant)
If at first you don't succeed , try doing something differently.
BruceS
BruceS
-
- Posts: 84
- Joined: Wednesday 22nd August 2018 7:55am
Re: Trapping Return - Serious problem ?
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 :
So I came up with this:
First, create a form with on it:
- Button1
- Button2
- Label1
I added catching ASCII codes as this might also come in handy.
What’s your verdict?
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 :
Public Sub Form_KeyPress()
Dim varKey As Variant
varKey = Key.Code
Me.Caption = "Last key: " & varKey
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
' Gambas class file
Public varKey As Variant
Public Sub Form_Open()
Me.caption = "Catch that key !"
End
Public Sub Form_KeyPress()
varKey = Key.Code
Me.caption = Key.text
If Button1.HasFocus Then
Button1.Text = "Last key: " & varKey & " ASCII = " & Str$(Asc(Key.Text))
Button2.text = ""
Endif
If Button2.HasFocus Then
Button2.Text = "Last key: " & varKey & " ASCII = " & Str$(Asc(Key.Text))
Button1.text = ""
Endif
Select Case varKey ‘some examples
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!"
Case Else
Label1.text = "No, try another one"
End Select
End
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.
You eat an elephant one small bite at a time.
Re: Trapping Return - Serious problem ?
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.Doctor Watson wrote: ↑Thursday 15th April 2021 6:49am ...Keypoint was this little code from Steve...
...What’s your verdict?
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.
Re: Trapping Return - Serious problem ?
...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!
<F1> returns 16777264
<Return> returns 16777220
<AltGr> returns 16781571
<Alt> returns 16777251
...and so on!
- BruceSteers
- Posts: 1574
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Trapping Return - Serious problem ?
That will be fine for your own programs that will only run on your computer.Doctor Watson wrote: ↑Thursday 15th April 2021 6:49am 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?’
Public Sub Form_KeyPress() Dim varKey As Variant varKey = Key.Code Me.Caption = "Last key: " & varKey End
So I came up with this:
First, create a form with on it:
- Button1
- Button2
- Label1
This enables me to catch any key at any moment. Well, almost any as you may find out.
' Gambas class file Public varKey As Variant Public Sub Form_Open() Me.caption = "Catch that key !" End Public Sub Form_KeyPress() varKey = Key.Code Me.caption = Key.text If Button1.HasFocus Then Button1.Text = "Last key: " & varKey & " ASCII = " & Str$(Asc(Key.Text)) Button2.text = "" Endif If Button2.HasFocus Then Button2.Text = "Last key: " & varKey & " ASCII = " & Str$(Asc(Key.Text)) Button1.text = "" Endif Select Case varKey ‘some examples 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!" Case Else Label1.text = "No, try another one" End Select End
I added catching ASCII codes as this might also come in handy.
What’s your verdict?
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..
http://gambaswiki.org/wiki/comp/gb.qt4/key
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.
Last edited by BruceSteers on Thursday 15th April 2021 2:25pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
-
- Posts: 84
- Joined: Wednesday 22nd August 2018 7:55am
Re: Trapping Return - Serious problem ?
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 http://gambaswiki.org/wiki/comp/gb.qt4/key 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
I’ll drink to that. Cheers!
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 http://gambaswiki.org/wiki/comp/gb.qt4/key 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
Public Sub Form_Open()
Me.caption = "Catch that key !"
End
Public Sub Form_KeyPress()
Select Case Key.Code
Case Key.F1
Label1.text = "Key pressed : F1"
Case Key.Return
Label1.text = "Key pressed : Return"
Case Key.Enter
Label1.text = "Key pressed : Enter"
Case Key.AltKey
Label1.text = "Finally! The Alt key!"
Case Key.Tab
Label1.text = "Tab key pressed"
Case Else
Label1.text = "You pressed : " & Key.text & " - ASCII " & Str$(Asc(Key.Text))
End Select
End
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.
- BruceSteers
- Posts: 1574
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Trapping Return - Serious problem ?
You got it fella.Doctor Watson wrote: ↑Thursday 15th April 2021 1:31pm 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 http://gambaswiki.org/wiki/comp/gb.qt4/key 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
And this will work on every computer? Well, one who runs Linux that is.
Public Sub Form_Open() Me.caption = "Catch that key !" End Public Sub Form_KeyPress() Select Case Key.Code Case Key.F1 Label1.text = "Key pressed : F1" Case Key.Return Label1.text = "Key pressed : Return" Case Key.Enter Label1.text = "Key pressed : Enter" Case Key.AltKey Label1.text = "Finally! The Alt key!" Case Key.Tab Label1.text = "Tab key pressed" Case Else Label1.text = "You pressed : " & Key.text & " - ASCII " & Str$(Asc(Key.Text)) End Select End
I’ll drink to that. Cheers!
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.
If at first you don't succeed , try doing something differently.
BruceS
BruceS