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

Discussion in 'Android Questions' started by wonder, May 19, 2015.

  1. wonder

    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?
    Code:
    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: May 19, 2015
  2. DonManfred

    DonManfred Expert Licensed User

    Do you get another Keycode in this event when you press multiple buttons? Put a log(KeyCode) to check

    Please note: a Keycode is ONE value.
    Maybe you get an combined value if you press two buttons at once

    For example:
    X = 10
    Y = 20

    Keycode then is maybe 30?
     
    wonder likes this.
  3. wonder

    wonder Expert Licensed User

    Unfortunately no, the KeyCodes are detected one by one.
     
  4. Peter Simpson

    Peter Simpson Expert Licensed User

    wonder likes this.
  5. DonManfred

    DonManfred Expert Licensed User

    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 likes this.
  6. wonder

    wonder Expert Licensed User

    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.

    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. :)
     
  7. DonManfred

    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
     
    Peter Simpson likes this.
  8. Peter Simpson

    Peter Simpson Expert Licensed User

    Sorry I didn't see the word Key Press whilst cruising on my phone. I would use a timer...
     
  9. wonder

    wonder Expert Licensed User

    Done! :D

    I didn't use the timer, though. This code works like a charm.
    Code:
    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) = -1Then
            
    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:
    Code:
    If Controller.KeyBuffer = "UR" Or Controller.KeyBuffer = "RU" Then Log("DPAD Direction: NE")
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice