B4J Question keycodes

I copied this code right from the B4X Language Booklet. I get this error:

Error parsing program.
Error description: Undeclared variable 'keycodes' is used before it was assigned any value.
Error occurred on line: 29 (Main)
If KeyCode = KeyCodes.KEYCODE_BACK Then

I am "trying" to learn this VB Dialect and i have no idea how to solve this problem.

Any help would be appreciated.

Charlie

get a keycode:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
Private Answ As Int
Private Txt As String

If KeyCode = KeyCodes.KEYCODE_BACK Then ' Checks if KeyCode is BackKey
    Txt = "Do you really want to quit the program ?"
    Answ = Msgbox2(Txt,"A T T E N T I O N","Yes","","No",Null)' MessageBox
    If Answ = DialogResponse.POSITIVE Then ' If return value is Yes then
        Return False ' Return = False the Event will not be consumed
    Else ' we leave the program
        Return True ' Return = True the Event will be consumed to avoid
    End If ' leaving the program
End If
End Sub
 

klaus

Expert
Licensed User
Longtime User
Can you post your test program as a zip file ?

1719900258742.png


Attached a small test program, TestKeycode, showing that the code works as expected.

And a test program with B4XPages, TestKeycode1, as suggested by Erel, which works for all platforms.
 

Attachments

  • TestKeycode.zip
    9.3 KB · Views: 87
  • TestKeycode1.zip
    14.1 KB · Views: 81
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
I copied this code right from the B4X Language Booklet. I get this error:

Error parsing program.
Error description: Undeclared variable 'keycodes' is used before it was assigned any value.
Error occurred on line: 29 (Main)
If KeyCode = KeyCodes.KEYCODE_BACK Then

I am "trying" to learn this VB Dialect and i have no idea how to solve this problem.

Any help would be appreciated.

Charlie

get a keycode:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
Private Answ As Int
Private Txt As String

If KeyCode = KeyCodes.KEYCODE_BACK Then ' Checks if KeyCode is BackKey
    Txt = "Do you really want to quit the program ?"
    Answ = Msgbox2(Txt,"A T T E N T I O N","Yes","","No",Null)' MessageBox
    If Answ = DialogResponse.POSITIVE Then ' If return value is Yes then
        Return False ' Return = False the Event will not be consumed
    Else ' we leave the program
        Return True ' Return = True the Event will be consumed to avoid
    End If ' leaving the program
End If
End Sub
Apparently,Keycodes is a predefined constants in B4A, there is not Keycodes in B4J.
if you want to quit App, do that as suggested by Erel. or add a keypress event to handle it
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I did not look carefully enough, I thought it was for B4A, and as already mentioned, the code you copied is only for B4A.
And as Erel posted, you might move to B4XPages.
The code in the TestKeycode1 works for B4A and B4J, not tested with B4i.
 
Upvote 0
Keycodes is a built-in type. Not sure why you see this warning. I don't see it here.

However I wouldn't use this code.
Better to start with B4XPages and if you want to handle the back key: https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-743793
i am NOT trying to end the program. I am trying to get "keypress" to work. I am writing this in B4J. I cannot take the time to learn about B4X, B4A I just want to "test" a routine to "see" how to trap a keypress. I thought for sure the routine in the manual would work, it doesn't say "only works in B4X pages".
 

Attachments

  • Try00.zip
    9.3 KB · Views: 80
Upvote 0

klaus

Expert
Licensed User
Longtime User
Upvote 0
I am actually trying to "port" a VB6 program to B4J in the program there i check for the user pressing the enter key:

Catch the Enter Key:
Private Sub txtNumber_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        ' Enter key was pressed
        ' Add your code to handle the Enter key press here
        'MsgBox "Enter key pressed while in txtNumber!"
        ' Optionally, you can consume the Enter key press by setting KeyCode to 0
        ' This prevents the default behavior of moving to the next control
        KeyCode = 0
        Call cmdGuess_Click
    End If
End Sub

I do this routine in almost every routine I've written. The user could enter a response (in a text box for instance) however this is cumbersome when the user has to enter "lots" of data, so I allow the user to press the enter key and then I call the routine "as if" the user has clicked on the appropriate button. So I thought to get familiar with "Keypress" I would start with an example. I am not trying to terminate the program I am trying to trap a key press... In the code the name of the subroutine is the name of the (in this case) control (txtNumber) After the underscore is the "event" (in this case KeyDown). The KeyCode is an integer representing a key that was pressed. The Shift parameter I never use but this specifies if (among other actions) the shift key was pressed while the keyboard key was pressed.

The line
If KeyCode = vbKeyReturn Then
checks to see if the Return (Enter) key was pressed. All "retrievable" keys have a name prefaced by vbKey.

The Line
KeyCode = 0
"consumes" the keypress (stops the system from processing the keypress)

Finally the line
Call cmdGuess_Click
Calls the "click event" of the cmdGuess button.

So this is basically a six line VB6 subroutine I am trying to get to work in B4J

charlie
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't see the problem here, but maybe I am seeing the wrong problem.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    
    Private lblKey As Label        
    Private gvh As GameViewHelper
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    gvh.AddKeyListener("gvh", MainForm)
End Sub


Private Sub gvh_KeyPressed (KeyCode As String) As Boolean
    Log(KeyCode)
    lblKey.Text = "Keycode is " & KeyCode
    Return True
End Sub
 

Attachments

  • KeyListener.zip
    2 KB · Views: 83
Upvote 0
I don't see the problem here, but maybe I am seeing the wrong problem.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
   
    Private lblKey As Label       
    Private gvh As GameViewHelper
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    gvh.AddKeyListener("gvh", MainForm)
End Sub


Private Sub gvh_KeyPressed (KeyCode As String) As Boolean
    Log(KeyCode)
    lblKey.Text = "Keycode is " & KeyCode
    Return True
End Sub
Thank you, finally something that works! However I need to know what control was being accessed. I added two textfields to your example and it doesn't matter where I press a key the gvh_KeyPressed returns "anywheresoftware.b4j.object.GameViewHelper@3406926d", So how can I tell what control has focus? (I also have a small problem, how would I "know" GameViewHelper should have been used to capture a keypress?)
Catch the Enter Key:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    
    Private lblKey As Label
    
    Private gvh As GameViewHelper
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout2")
    MainForm.Show
    gvh.AddKeyListener("gvh", MainForm)
End Sub


Private Sub gvh_KeyPressed (KeyCode As String) As Boolean
    Log(KeyCode)
    
    Log(Sender)

    lblKey.Text = "Keycode is " & KeyCode
    If KeyCode = "Enter" Then
        Log("Return Pressed")
    End If
    Return True
End Sub

Any help would be appreciated.

Charlie
 

Attachments

  • KeyListener.zip
    3.4 KB · Views: 80
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
However I need to know what control was being accessed.
The key listener is simply listening to the keys, just like it says on the tin. It is oblivious to whatever else is happening. If you need to keep track of which control has the focus then that is an independent task. I think that means using "focus-changed" events for all the views of interest and tracking which one took the focus most recently.

How would I "know" GameViewHelper should have been used to capture a keypress?
Yes - I am with you 100% there. I cannot remember how I discovered that secret.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Since you are learning also look at Sender. Every (most) events have been raised by a control (view) and IN THE EVENT you can GET the View that raised the event using Sender.
B4X:
Sub Buttons_Click
  Dim btn [As Button
  btn = Sender  ' sets the buton that raised the event to btn
  btn.Text=Rnd(1,50)
End Sub
B4X:
Private Sub MyEditText_FocusChanged (HasFocus As Boolean)
    Dim et as EditText = Sender
    Log(et.Text)
End Sub
In this way you can create ONE EVENT for ALL of your views.

And get which one is sending the message using sender.
1722433557977.png

Also, if you use a B4XFloatTextField view it HAS an EnterPressed event already so you don't have to test for that. As mentioned, you can set the event name for all of your B4XFloatTextFields to the same event, GET the one that triggered the event using Sender and then take the appropriate action.

Another big difference over VB6 that takes some getting used to when you get the View from Sender it has no name - java views just don't have names. Generally the solution is to use the Tag property to store the ID of the control. So:
B4X:
Private Sub Mytextfield_EnterPressed
    Dim MTC as B4XFloatTextField = Sender
    If MTC.tag = "NameField" Then
    .
    .
    
End Sub

I came from years of VBA - which I still do - but B4X is much more fun. The hardest part here (unlike VB5/VBA) is there are SO many ways to skin the same cat.

The upside is there is SO much more you can do.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
so I allow the user to press the enter key and then I call the routine "as if" the user has clicked on the appropriate button
I may be missing something here and can't check this now, but I think that if all you want to do is capture the enter key in a text field in s B4J app then you an use the Action event.

B4X:
Sub TextField_Action
    Log("Enter Key Pressed")
    '....
End Sub

As @MrKim describes above, you can set each text field's event name in the designer. If you set the event name the same for all the textfields then you can use the Sender object to find out which one was 'actioned'.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
? for b4J


See example b4j (Login user) key Escape
Note:
You can extend the use of keys or combinations of them, associate them with a view or object
1722510579271.png
 

Attachments

  • signin_test2.zip
    5.8 KB · Views: 50
Last edited:
Upvote 0
Top