Android Question Shift+TAB combination on an external keyboard

mw71

Well-Known Member
Licensed User
Longtime User
Hi,

Is it possible to query the Shift+TAB combination on an external keyboard?
I can query the TAB key via Activity_KeyPress -> If KeyCode = KeyCodes.KEYCODE_TAB then.....,
but no KeyCode appears for Shift (Sub Activity_KeyPress (KeyCode As Int) As Boolean -> Log("Key Pressed: " & KeyCode)).
 

mw71

Well-Known Member
Licensed User
Longtime User
That's my problem.
When I press the Shift key, Activity_KeyPress is not triggered.

Shift-TAB and TAB return the same key code.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Here's what ChatGPT says about this.
I haven't tested the code so you'll have to check it yourself...

In B4J, you capture keyboard events with the KeyPressed and KeyReleased events from a Form or a Node.

The Shift key is treated like any other key, but note that modifier keys (Shift, Ctrl, Alt) don’t always give you a character value – instead you detect them using the KeyCode.

Example​

Sub Form1_KeyPressed (EventData As KeyEvent)
Log("KeyPressed: " & EventData.KeyCode & " Text=" & EventData.Text)
If EventData.KeyCode = fx.Keys.SHIFT Then
Log("Shift pressed")
End If
End Sub

Sub Form1_KeyReleased (EventData As KeyEvent)
Log("KeyReleased: " & EventData.KeyCode)
If EventData.KeyCode = fx.Keys.SHIFT Then
Log("Shift released")
End If
End Sub

What you’ll see​

  • When Shift is pressed down:
    EventData.KeyCode = 16 (the constant fx.Keys.SHIFT).
    EventData.Text will usually be "" (empty).
  • When Shift is released:
    EventData.KeyCode = 16 again.
So the values are:
  • Down = 16
  • Up = 16
👉 If you want to check whether Shift is being held while another key is pressed, you can use:

If EventData.ShiftDown Then
Log("Shift + " & EventData.Text)
End If

Do you want me to show you a minimal B4J example where pressing Shift alone logs, and pressing Shift+Letter logs "Shift + Letter"?
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Sorry, I got confused about that.
This is what I got from ChatGPT for B4A:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Log("KeyPress: " & KeyCode)
    If KeyCode = KeyCodes.KEYCODE_SHIFT_LEFT Or KeyCode = KeyCodes.KEYCODE_SHIFT_RIGHT Then
        Log("Shift pressed")
        Return True   ' consume the event (optional)
    End If
    Return False      ' let system handle it
End Sub

Sub Activity_KeyUp (KeyCode As Int) As Boolean
    Log("KeyUp: " & KeyCode)
    If KeyCode = KeyCodes.KEYCODE_SHIFT_LEFT Or KeyCode = KeyCodes.KEYCODE_SHIFT_RIGHT Then
        Log("Shift released")
        Return True
    End If
    Return False
End Sub
Is this anything like you have?
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
And ChatGPT gives this code:

👉 If you need to test whether Shift is held while another key is pressed, you can check inside Activity_KeyPress:
B4X:
If (KeyData.MetaState And 1) <> 0 Then   ' META_SHIFT_ON = 1
    Log("Shift is being held")
End If
 
Upvote 0

mw71

Well-Known Member
Licensed User
Longtime User
As I have now learned, KeyCodes.KEYCODE_SHIFT_LEFT or KeyCode = KeyCodes.KEYCODE_SHIFT_RIGHT unfortunately do not work when an EditText box is active.
Outside of this, e.g., immediately after startup, it works. The EditText box seems to “consume” the shift.

And ChatGPT gives this code:

👉 If you need to test whether Shift is held while another key is pressed, you can check inside Activity_KeyPress:
B4X:
If (KeyData.MetaState And 1) <> 0 Then   ' META_SHIFT_ON = 1
    Log("Shift is being held")
End If
What is KeyData and how do I access it? That seems to be from B4J?
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
As I have now learned, KeyCodes.KEYCODE_SHIFT_LEFT or KeyCode = KeyCodes.KEYCODE_SHIFT_RIGHT unfortunately do not work when an EditText box is active.
Outside of this, e.g., immediately after startup, it works. The EditText box seems to “consume” the shift.


What is KeyData and how do I access it? That seems to be from B4J?
That I'm not sure my friend, I got it from ChatGPT.
A lot of the time, the ChatGPT results include code from Java. It has in mind (?) that it knows B4A and B4J are java based and therefore give results accordingly. That's what I've found and had to correct it many times. In the end it gives the correct code if you bear with it, ot something close.

Do you use any AI software? If not, create an account on one and play around with it.
 
Upvote 0

mw71

Well-Known Member
Licensed User
Longtime User
That I'm not sure my friend, I got it from ChatGPT.
A lot of the time, the ChatGPT results include code from Java. It has in mind (?) that it knows B4A and B4J are java based and therefore give results accordingly. That's what I've found and had to correct it many times. In the end it gives the correct code if you bear with it, ot something close.

Do you use any AI software? If not, create an account on one and play around with it.
- First, I tried it myself.
- Then, I used the search engine and the forum.
- Since that didn't yield any results, I consulted ChatGPT and Gemini. The "solutions" didn't work.
(The statement that Shift is consumed in Edit Text comes from ChatGPT!
I can't verify it, but it fits with the observations I made during my test and sounds plausible to me.)
- Only after that did I ask here in the forum.

Please note that large language models (such as ChatGPT) work by guessing, so they make mistakes.
That may sound strange at first, but it's true. (We had to do a training session at work on the topic.)
Sometimes these mistakes are obvious, such as the use of non-existent commands,
and sometimes you notice them during testing.

I have access to the standard, somewhat limited AI models (unless you pay) and to the unlocked ChatGPT 5.
 
Upvote 0
Top