Trapping Return - Serious problem ?

Post your Gambas programming questions here.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Trapping Return - Serious problem ?

Post by BruceSteers »

Also there is this way. (Using Key["Return"] method not Key.Return constant)

Select Key.Code
   Case Key["Return"], Key["Enter"]
   Print "Enter or return was hit"

  Case Key["F1"]
  Print "F1 was hit"

End Select
If at first you don't succeed , try doing something differently.
BruceS
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Trapping Return - Serious problem ?

Post by Doctor Watson »

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 :
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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Trapping Return - Serious problem ?

Post by stevedee »

Doctor Watson wrote: Thursday 15th April 2021 6:49am ...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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Trapping Return - Serious problem ?

Post by stevedee »

...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!
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Trapping Return - Serious problem ?

Post by BruceSteers »

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 :
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?
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..
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
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Trapping Return - Serious problem ?

Post by Doctor Watson »

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
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.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Trapping Return - Serious problem ?

Post by BruceSteers »

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
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!
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.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply