Android Question How Know if a pressed Key come from Bluetooth or Keyboard

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

Anyway to identify wich device pressed key come? in ACTIVITY_KEYPRESS ?


I try identifing is keyboard is visible, but not works very well

Sub HardwareKeyboardPresent As Boolean
Dim ref As Reflector
ref.Target = ref.GetContext
ref.Target = ref.RunMethod("getResources")
ref.Target = ref.RunMethod("getConfiguration")
Dim keyboard As Int = ref.GetField("keyboard")
Return keyboard <> 1
End Sub


And for the user, the pressed key is configurable, I mean, he can press any button in a bluetooth device, I need to know if come from bluetooth device or no.
 

stevel05

Expert
Licensed User
Longtime User
According to the documentation
there is no guarantee that any key press on a soft keyboard will generate a key event
so you may have to experiment with the bluetooth device to see what that sends. It may still give you a solution.

There is a lot of information in the key event which you can access using JavaObject.
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Stevel05,

I can intecerpt the keycode inside a textedit or textview, but in a entire activity, the sub "key_input", look the code:

Dim objRef As Reflector
objRef.Target = Activity
objRef.SetOnKeyListener("Key_input")


In my screen i don´t have any textedit... imagine you have only a activity and you waiting a pulse of button from your bluetooth device and you need to know is coming from bluetooth device.

Obs1: It´s working OK inside a textedit
Obs2: I have the reflector 2.4 installed..
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Unfortunately I don't have a bluetooth device to test it with. There are some posts on the internet that talk about setting up a broadcast receiver for MEDIA_BUTTON. You may want to look at that as a solution, although I'm not sure that it will differentiate between the bluetooth device and the devices own media keys.

Someone else on the forum may have already looked at this, hopefully they will reply.
 
Upvote 0

Saverio

Member
Licensed User
Longtime User
Hello Alberto,

I'm working with something different than your job, but got the same issue.
It seems impossible catch the OnKey event with Activity View using Reflection:

r.target = Activity
r.SetOnKeyListener("SomeSub")

It for me don't work at all.
Yes, I know, I could use KeyDown and KeyUp event.
But don't know how get them inside a class module.

I'v tried several ways with Reflection and JavaObject as well.
Til now got no result.

If there is some good soul that can throw some rope,
I will be grateful with him. ;)

Saverio
 
Upvote 0

Saverio

Member
Licensed User
Longtime User
Nice.

I also tried that.
But as you said it's dirty
You have to control the software keyboard, and, it's almost impossible control the hardware keys properly.

So, in the weekend, I tried a lot of other things for my project.
What's come out is:
B4X:
Sub Activity_Create

  Dim re As Reflector
  re.Target = Activity
  re.SetOnKeyListener("ActivityOnKey")
  re.RunMethod2("setFocusable", "True",  "java.lang.boolean")
  re.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
  Activity.RequestFocus

End Sub

Private Sub ActivityOnKey(vTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean

  re.Target = KeyEvent
  'Dim Action As Int = re.GetField("mAction") 'Same result but the field name could change
  Dim Action As Int = re.RunMethod("getAction")

  Select Action

  Case Activity.ACTION_DOWN
    'Do SomeThing                
    'Return(True) to cancel the event

  Case Activity.ACTION_UP
    'Do Somethig
     'Return(True) to cancel the event
  End Select
 
  Return(False)
End Sub


Give it a try. It will also work with Panel and some other View derivate.
If you need, there are more info inside the KeyEvent Object.
Just stop the app execution, using "Debug(rapid)" feature, inside ActivityOnKey event and watch,
by B4A watcher, the KeyEvent Object.
If you want to know the other methods that can use with RunMethod, you can look at this.

For me it's working also inside a class.
I also have a bluetooth keyboard, but don't had time to try it. I will do it soon.

Hope it's useful for you too

Saverio
 
Last edited:
Upvote 0
Top