Android Question [SOLVED] How to detect more than one KeyPress at the same time?

wonder

Expert
Licensed User
Is it possible to detect more than one KeyPress at the same time?

Now, obviously this code doesn't work. Is there a way to do this?
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_DPAD_UP And KeyCode = KeyCodes.KEYCODE_DPAD_UP Then
        Log("DPAD Direction: NE")
    End If
    Return True
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
KeyCodes are detected one by one
Then the logic says to you that you cannot combine two different values when you only get TWO events with one keycode and you want to check two. It´s not logically...

What you can try is to save the last keycode and if another keycode event is raised within a given time then you can check both...

Just my two cent (based on the double-tap-to-close class)
 

wonder

Expert
Licensed User
I once read this Bruno, but I do not think that http://www.b4x.com/android/forum/threads/android-multitouch-tutorial.10419/#contentit is what you are after...
Thanks! That's actually quite useful, but in this case, I'm looking for a way to catch two or more hardware keys pressed at the same time.

Then the logic says to you that you cannot combine two different values when you only get TWO events with one keycode and you want to check two. It´s not logically...

What you can try is to save the last keycode and if another keycode event is raised within a given time then you can check both...

Just my two cent (based on the double-tap-to-close class)
Exactly my thoughts. I should be able to create a KeyBuffer which returns True once two specific KeyCodes have been entered (no matter which order).

I'll share the code once I got it figured out. :)
 

DonManfred

Expert
Licensed User
Use a short timer and a list which holds keypresses. After the first press you store the value in the holding list and start a timer. It the time fires before another keypress event raises then remove the last entry from the list and stop the timer.... If not then you have the actual KeyCode and the one in the holding list. Now you can compare the two

Just my thoughts
 

wonder

Expert
Licensed User
Done! :D

I didn't use the timer, though. This code works like a charm.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
        If KeyCode = Controller.KEYCODE_DPAD_LEFT AND Controller.Btn_Press <> Controller.KEYCODE_DPAD_LEFT Then
            Controller.Btn_Press = Controller.KEYCODE_DPAD_LEFT
            Controller.KeyBuffer = Controller.KeyBuffer & "L"
        End If

        If KeyCode = Controller.KEYCODE_DPAD_RIGHT AND Controller.Btn_Press <> Controller.KEYCODE_DPAD_RIGHT Then
            Controller.Btn_Press = Controller.KEYCODE_DPAD_RIGHT
            Controller.KeyBuffer = Controller.KeyBuffer & "R"
        End If
     
        If KeyCode = Controller.KEYCODE_DPAD_UP AND Controller.Btn_Press <> Controller.KEYCODE_DPAD_UP Then
            Controller.Btn_Press = Controller.KEYCODE_DPAD_UP
            Controller.KeyBuffer = Controller.KeyBuffer & "U"
        End If

        If KeyCode = Controller.KEYCODE_DPAD_DOWN AND Controller.Btn_Press <> Controller.KEYCODE_DPAD_DOWN Then
            Controller.Btn_Press = Controller.KEYCODE_DPAD_DOWN
            Controller.KeyBuffer = Controller.KeyBuffer & "D"
        End If
End Sub

Sub Activity_KeyUp (KeyCode As Int) As Boolean
        If KeyCode = Controller.KEYCODE_DPAD_LEFT Then
            Controller.removeKey("L")
        End If

        If KeyCode = Controller.KEYCODE_DPAD_RIGHT Then
            Controller.removeKey("R")
        End If
     
        If KeyCode = Controller.KEYCODE_DPAD_UP Then
            Controller.removeKey("U")
        End If

        If KeyCode = Controller.KEYCODE_DPAD_DOWN Then
            Controller.removeKey("D")
        End If
End Sub

'Removes a KeyString from the controller's KeyBuffer
Sub removeKey(KeyString As String)
    If Not(Controller.KeyBuffer.IndexOf(KeyString) = -1) Then
        Dim Part1, Part2 As String
        For i = 0 To (Controller.KeyBuffer.IndexOf(KeyString) - 1)
            Part1 = Part1 & Controller.KeyBuffer.CharAt(i)
        Next
        For i = (KeyBuffer.IndexOf(KeyString) + KeyString.Length) To (KeyBuffer.Length - 1)
            Part2 = Part2 & Controller.KeyBuffer.CharAt(i)
        Next
        Controller.KeyBuffer = Part1 & Part2
    End If
End Sub
Later on, all I'll have to do is:
B4X:
If Controller.KeyBuffer = "UR" Or Controller.KeyBuffer = "RU" Then Log("DPAD Direction: NE")
 
Top