Android Question keycodes.

in my testing i wanted to check a keypress for the enter (return) key. The documentation tells me:

KeyCode is the code of the pressed key, you can get them with the KeyCodes keyword.

so to make my life easier i typed in:

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

right out of the documentation. I get an error on KeyCodes.

which is:

Main - 50: Undeclared variable 'keycodes' is used before it was assigned any value.

So what a want is the mnemonic for the return key putting my cursor over KeyCodes yields a popup with the error. Am I missing a library? What am i doing wrong? When i do find the mnemonic for the return key I will use a statement similar to the IF statement in the sample quote and that is telling me the sample doesn't work. Why can't i get a list of the mnemonics.

Charlie
 

RichardN

Well-Known Member
Licensed User
Longtime User
Charlie....

If you write code in a scrawl without indentations to highlight each clause it is both unreadable and, by definition, un-debuggable. Use CODE /CODE tags when posting.

Main - 50: Undeclared variable 'keycodes' is used before it was assigned any value.

The variable 'keycodes' is not the same thing as the 'KeyCode' passed to the procedure.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
By the way that code is working.
You should export and post your project for us to check where something it's not working.
You should start making B4XPages project instead of Default.
It's not mandatory, but B4XPages will avoid you many headaches when going to develop more sofisticated apps.
Of course in the forum you will find mixed examples, older ones are based on Activity while newer are usually based on B4XPages, and this can generate some confusion in new members.
But there will always be someone to help and answer questions.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The variable 'keycodes' is not the same thing as the 'KeyCode' passed to the procedure.
It is another thing.

Keycodes is a static android class which probably needs to be added to the imports first.
 
Upvote 0
keycodes:
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

the problem is the first if

If KeyCode = KeyCodes.KEYCODE_BACK Then

Notice KeyCodes IS different from KeyCode!
By the way that code is NOT working.

This example is right out of the documentation. I get an error on KeyCodes.

which is:

Main - 50: Undeclared variable 'keycodes' is used before it was assigned any value.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I tested it.
I will post the project here when possible.
KEYCODE is the variable that contain the key that raised the event.
KEYCODES is the command that contain all the constants of the possible keypress to decide which one search for.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
@charlieelliott785@hotmail
In this example I just made Copy/Paste with the code you posted, and it works.
Let us know if it work for you too.
 

Attachments

  • Keycodes.zip
    9.2 KB · Views: 73
Upvote 0
thanx for your help. However I was not using B4A I thought I was using B4X but alas i was in the forum as there is no B4X i thought any form would do I was wrong. Anyhow this problem needs to be moved to B4J, and it still doesn't work.

Charlie
 
Upvote 0
Top