Android Question Issue with Right click on a mouse

NeoTechni

Well-Known Member
Licensed User
Longtime User
So half of this question has been addressed before.
When you right-click on a mouse, it acts as the back button.
Previous topics have addressed that by blocking the back button.

I don't want to do that though. I only want to detect when the mouse is clicked and ONLY block right click from acting as the back button.
This is done in Android by checking GetSource() of the (event as KeyEvent) parameter in the keyup/down subs. But we don't have access to that parameter. How would I go about getting it?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I imagine you can use JavaObject to get it. I'm not an expert on jO, but I guess it would be something like:

B4X:
Sub Activity_KeyUp (KeyCode As Int) As Boolean
    Private input as JavaObject = Sender
    Private btn as Int = input.RunMethod("getSource", Null)
End Sub

Note that you will need the JavaObject library available here -> JavaObject library.

I can't test this right now because I'm doing an Xcode update on my Mac partition & can't boot into Windows until it's done.

- Colin.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Funny, I can't get the keyup event to fire.
I put the code in the keypress event but that didn't work.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Funny, I can't get the keyup event to fire.
I put the code in the keypress event but that didn't work.
Hmmm - sorry, I can't test it myself so I don't know what to suggest. Did you get an error? What was the value of btn after the RunMethod returned?

- Colin.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
No error or value, the event won't fire at all.

But it can wait. I have tons of other work to do.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Been there. Not what I'm looking for.
But aren't you just trying to detect when there is a right click? If you only want to block it from acting as a back button then perform the action you want, then return true.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Select KeyCode
        Case4
            Do whatever
            Return True
     End Select
     Return False
End Sub

- Colin.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Previous topics have addressed that by blocking the back button.

I don't want to do that though. I only want to detect when the mouse is clicked and ONLY block right click from acting as the back button

So I need the source, keypress doesn't have it. Keyup does but isn't firing.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I think it's cause I use a panel over top of the activity (so I can have a camera preview, and draw over top of it) the panel doesn't have the keyup event. Any ideas?

EDIT: got the event to fire by removing the return false from keypress

But
B4X:
Private input as JavaObject = Sender
Private btn as Int = input.RunMethod("getSource", Null)

isn't working. I get "method: getSource not found in activitywrapper"
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
That's got me on the right track, but how do I call a B4A sub from within the Java?

B4X:
#If JAVA
public boolean _onkeydown (int keyCode, android.view.KeyEvent event) {
   BA.Log("onkeydown");
   BA.Log(Integer.toString(keyCode));
   BA.Log(Integer.toString(event.getSource()));
   
   Activity_KeyDown(keyCode, event.getSource());
   return true;
}
#End If

Sub Activity_KeyDown(keyCode As Int, Source As Int)
   Log("Activity_KeyDown: " & keyCode & " Source : " & Source )
End Sub

I've also tried:

B4X:
BA.Activity_KeyDown(keyCode, event.getSource());
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Got it:

B4X:
#If JAVA
public boolean _onkeydown (int keyCode, android.view.KeyEvent event) {
   anywheresoftware.b4a.keywords.Common.CallSubNew3(mostCurrent.activityBA,main.getObject(),"Activity_KeyDown", keyCode, event.getSource());
   return true;
}
#End If
Sub Activity_KeyDown(keyCode As Int, Source As Int)
   Log("Activity_KeyDown: " & keyCode & " Source : " & Source )
End Sub
 
Upvote 0
Top