simulate hardware keypress

Jondroid

Member
Licensed User
Longtime User
Dear all,
for various reasons I would like to simulate a keyevent. I know the right Keycode and I read how to do it over adb. But now I want to do it from within an application. However, I couldn't find a "native" way, so I thought, why not doing it by calling the shell.
So in Activity_Create I execute:

B4X:
Dim p As Phone
p.Shell("input",Array As String("keyevent", "25"),Null,Null)

However, the app just freezes.

What is the proper way to send keyevents (not to respond to a keypress but to simulate a keyevent?`

Many thanks in advance,
Jondroid
 

Jondroid

Member
Licensed User
Longtime User
Oh yes, and before I forget .. the reason why I used 25 as a keycode above is not that I actually want to change the volume in my final app but it's a nice button to press as one will (or better to say: should) directly see the pop-up which shows that the volume was changed.
So don't worry about telling me what other methods I could use to change the volume.. that's not the point of my little excercise but I want to find a way to simply simulate any keyevent.
 
Upvote 0

Jondroid

Member
Licensed User
Longtime User
Hi Margret,
Did you try this? It remains red in my editor and I get a compilation error:


Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'activity_keypress' is used before it was assigned any value.
Occurred on line: 35
Activity_KeyPress(25)
 
Last edited:
Upvote 0

Jondroid

Member
Licensed User
Longtime User
Erel:
Is the problem described above possible to be solved in Basic4Android?

I mean certainly in Java it is possible to simulate keyevents. I've seen "button saver" and other apps in the market which simulated hardware keypress.

I'm a great fan of B4A, mainly because it helps to rapidly prototype apps, however, I couldn't find any documentation regarding simulation of keyevents. It's fine if not everything is documented yet but then the importance of the forum becomes extremely high.

Many thanks.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Hello, yes I tried it and I use it in code. It requires another sub to handle the keypress. You have to code what you wish to happen. See code below as an Example:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
   If KeyCode = KeyCodes.KEYCODE_BACK AND hlpfl=1 Then
      hlpfl=0
      myw.Visible = False
      Return True
   End If
   If KeyCode = KeyCodes.KEYCODE_BACK Then
        If Msgbox2("Please Confirm Your Choice?", "Exit APP", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
            If mydb.pointer > -1 AND mydb.RecordCount > 0 Then
            mydb.UpdateRecord
         End If
         Return False
        Else
         Return True
        End If
    End If
End Sub
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
There's something you didn't explain: what app will receive the key event ? The system, another app, your app ? If it's in your app, it's probably not the best thing to do. There are surely other ways. If you want to send your key to another app, that's a very different thing.
 
Upvote 0

Jondroid

Member
Licensed User
Longtime User
Hi Margret, thanks for your reply. Well, actually in your code you're not actually generating a real keyevent. You just call another function with one parameter. The fact that you call that parameter "keycode" makes no big difference. So I'm afraid but this is not really generating a keyevent.
 
Upvote 0

Jondroid

Member
Licensed User
Longtime User
Hi Informatix,
well, I plan to send it to the application which has the current focus. And of course that should happen from within a service module. E.g. the service module is waiting for a specific trigger and then it should send the keyevent to the application which is in the focus.
Very much like what happens if you do

B4X:
adb shell
$ input keyevent 25

So how could I do that? I mean my very first post shows my attempt to run that shell command from within my Android app.

And by the way, searching the forum shows a good number of threads which describe a similar problem but without a solution.

So am I right to assume that the limitations of B4A are so great that a simple app as "button saver" can't be developed using B4A?

Erel, I don't say this to offend you in any way, I greatly appreciate your development of B4A and in fact I've been recommending B4A on all sorts of levels to my coworkers and friends but from looking through the archive of posts I see that all threads remain unanswered. Including statements that you don't think that this is possible. However, the reality of apps on the Market prove that it is possible.

So I would greatly appreciate if someone can point me to the right direction. You see before I recommend to my line manager to use B4A I need to be convinced that in fact the community is there to help with problems where B4A meets its limitations.

Otherwise the bottom line would be that B4A can't be considered as a real alternative to writing it directly in Java.

Please get me right on this one. I just want to avoid spending hours and hours on a project and then finding out half-way through (or close to the end) that there is one limitation which is an intrinsic problem to B4A making it impossible to use B4A for the WHOLE project...
 
Upvote 0

Jondroid

Member
Licensed User
Longtime User
Hi Informatix,
that sounds very promising. Thank your for your reply. Sure... there are most likely very many problems between my keyboard and my chair... :)

Anyway, this link here may do in Java what I want to do... but then I have to learn a little bit of Java I guess to make the jump to convert this now to a B4A library.

Thanks again...
 
Upvote 0

Jondroid

Member
Licensed User
Longtime User
NEW library: generate Keyevents (keygen)

:sign0060: Thanks for the heads-up... at least it motivated me to give it a try and so I managed to writte a B4A library which does generate hardware keyevents.

Attached is the library and an example code. Documentation not done yet.

As usual you need to extract the keygen.zip into the "additional library" folder.


With that library one can then simply run

B4X:
keyg.keygenerator(keycodes.KEYCODE_VOLUME_DOWN)

(see example file for full code)
 

Attachments

  • keygen-example.zip
    6.4 KB · Views: 937
  • Keygen.zip
    1.7 KB · Views: 903
Upvote 0

warwound

Expert
Licensed User
Longtime User
LOL i'm surprised the forum software would let you upload a file named keygen.zip, i had to look twice before it clicked that this was your keygen lib.

Martin.
 
Upvote 0

java4never

Member
Licensed User
Longtime User
Hi!

Are there any library dependencies, except of threading?
I simply can't get it to work, also the example seems to be broken.

Error without treading: java exception: "This method can not be called from the main application thread."

Error with treading: "Sub .... not found" which is the Sub I referenced in the Thread.Start line.

Thanks in advance
David W.
 
Upvote 0

nicholas.jj.taylor

Member
Licensed User
Longtime User
that keygen is gonna bring some people to this forum... :eek:

I was hoping so too, but if you google for "B4A keygen", this forum is nowhere to be seen, and I'm sure that you can imagine the results I had returned as an alternative.

I was actually looking for this library, which probably suggests that the forum's SEO should be sharpened.
 
Upvote 0

nicholas.jj.taylor

Member
Licensed User
Longtime User
:sign0060: Thanks for the heads-up... at least it motivated me to give it a try and so I managed to writte a B4A library which does generate hardware keyevents.

Attached is the library and an example code. Documentation not done yet.

As usual you need to extract the keygen.zip into the "additional library" folder.


With that library one can then simply run

B4X:
keyg.keygenerator(keycodes.KEYCODE_VOLUME_DOWN)

(see example file for full code)

Which thread library do you use? The Threading library won't compile your example.

I'm looking forward to this library working. My hope is that a future version will allow calls from UI thread
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Hi!

Are there any library dependencies, except of threading?
I simply can't get it to work, also the example seems to be broken.

Error without treading: java exception: "This method can not be called from the main application thread."

Error with treading: "Sub .... not found" which is the Sub I referenced in the Thread.Start line.

Thanks in advance
David W.

I'm getting the same error (using it from a service)
 
Upvote 0
Top