Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Gamepad and Android


This question is answered.


Permlink Replies: 8 - Last Post: Oct 8, 2016 10:51 AM Last Post By: Marc Bonnaud
Marc Bonnaud

Posts: 14
Registered: 7/1/16
Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 4, 2016 5:15 AM
Still under Delphi pro Berlin 10.1 (Win 7 ultimate SP1), no Update 1 installed yet.

Hello,

I bought this mid-range VR heaset
http://media.ldlc.com/ld/products/00/03/54/28/LD0003542855_2.jpg
https://www.freeflyvr.com/

As you can see, it comes with a small bluetooth remote/gamepad.

After connection, it's recognized (as a keyboard) by my Samsung S7 phone or my NVidia shield K1 tablet, and each of the 8 buttons (the 4 for the direction and the four others) is recognized either in the Android menus or in many applications like Chrome.

If I check in a browser the site http://html5gamepad.com/ on the device it is connected to, the buttons are recognized with
axis 0 / -1 ; axis 0 / 1 (or rather 0.99) ; axis 1 / -1 ; axis 1 / 1 (or rather 0.91)
for the 4 direction buttons and
B0 / 1 ; B1/ 1 ; B2 / 1 ; B3 / 1
for the four other buttons (with a cross, a triangle...) often seen with A, B, X and Y on other gamepads.

The button at the bottom (in vertical mode), B1, serves as an escape/exit button in almost all android apps, B0 serves as validation/enter most of the time also.

Although this gamepad has a specific brand, it is obviously a quite common device, e.g. :
http://www.ebay.fr/itm/like/172030470237?lpid=97&chn=ps
http://www.dx.com/fr/p/wireless-bluetooth-game-controller-joystick-gaming-gamepad-for-android-ios-smart-phone-black-423926

Now, for Delphi.

First, in any Android Firemonkey app I've tested it with, the B1 button does not exit the app.

I made a small testing app with :

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
Label1.Text:=KeyChar; Label2.Text:=IntToStr(Ord(KeyChar)); Label3.Text:=UIntToStr(Key);
Memo1.Lines.Add('click');
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
Label1.Text:=KeyChar; Label2.Text:=IntToStr(Ord(KeyChar)); Label3.Text:=UIntToStr(Key);
Memo1.Lines.Add('click up');
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
Memo1.Lines.Add('mouse down');
end;

and same for FormMouseMove, FormMouseUp, FormMouseWheel, FormTap and FormTouch, to be sure I detect any relevant event.

For the direction keys, it's fine : I get the 37, 38; 39 and 40 codes.

But for the 4 other keys, B0 through B3, nothing happens with FormKeyDown, and "click up" appears in the Memo with FormKeyUp, but 0 for Key and no Keychar.

I've downloaded the game apps from https://github.com/EmbarcaderoPublic/DelphiArcadeGames since they are said to deal with gamepads by tethering.

But when I run the ("full") RocketShipDelta, the pad is not recognized, I press "Gamepad" and on the next screen : "You are not connected". I try to connect with the socket icon in the upper right corner, the progress bar... progresses but nothing happens. If I disconnect the gamepad with its start button or connect after starting the game, the app exits.

Any idea of how to tell which of the 4 buttons is clicked ?

If I could just tell in FormKeyUp which one of the four B0 thru B3 is pressed, that would be enough for my present needs...
Eli M

Posts: 1,346
Registered: 11/9/13
Re: Gamepad and Android
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 4, 2016 4:47 PM   in response to: Marc Bonnaud in response to: Marc Bonnaud
The App Tethering components from the games only connect to another device running App Tethering as well.

Not sure why it wouldn't be detecting keycodes from those buttons. It looks like your device has Bluetooth though. Maybe use TBluetooth or TBluetoothLE.

http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Bluetooth

also

https://developer.android.com/training/game-controllers/controller-input.html
https://w3c.github.io/gamepad/#gamepadbutton-interface

Can you use some other Android app that will tell you the keycodes when you press them?

Maybe have to add a helper to Delphi for it to handle those keys?

Check in:
FMX.KeyMapping

https://developer.android.com/reference/android/view/KeyEvent.html

int KEYCODE_BUTTON_1
Key code constant: Generic Game Pad Button #1.
int KEYCODE_BUTTON_10
Key code constant: Generic Game Pad Button #10.
int KEYCODE_BUTTON_11
Key code constant: Generic Game Pad Button #11.
int KEYCODE_BUTTON_12
Key code constant: Generic Game Pad Button #12.
int KEYCODE_BUTTON_13
Key code constant: Generic Game Pad Button #13.
int KEYCODE_BUTTON_14
Key code constant: Generic Game Pad Button #14.
int KEYCODE_BUTTON_15
Key code constant: Generic Game Pad Button #15.
int KEYCODE_BUTTON_16
Key code constant: Generic Game Pad Button #16.
int KEYCODE_BUTTON_2
Key code constant: Generic Game Pad Button #2.
int KEYCODE_BUTTON_3
Key code constant: Generic Game Pad Button #3.
int KEYCODE_BUTTON_4
Key code constant: Generic Game Pad Button #4.
int KEYCODE_BUTTON_5
Key code constant: Generic Game Pad Button #5.
int KEYCODE_BUTTON_6
Key code constant: Generic Game Pad Button #6.
int KEYCODE_BUTTON_7
Key code constant: Generic Game Pad Button #7.
int KEYCODE_BUTTON_8
Key code constant: Generic Game Pad Button #8.
int KEYCODE_BUTTON_9
Key code constant: Generic Game Pad Button #9.
int KEYCODE_BUTTON_A
Key code constant: A Button key.
int KEYCODE_BUTTON_B
Key code constant: B Button key.
int KEYCODE_BUTTON_C
Key code constant: C Button key.
Marc Bonnaud

Posts: 14
Registered: 7/1/16
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 5, 2016 5:07 PM   in response to: Eli M in response to: Eli M
Thanks for answering.

The problem is not with Bluetooth, the gamepad is already connected and detected by the Android menu, and apps, and the HTML5 gamepad tester site, even by my Delphi apps for the direction keys.

I tried an Android app called HWchecker

The direction keys give two code (down and up), for instance (this app writes from the bottom up on the screen, I will write here the natural way) :
KEYCODE_DPAD_DOWN(20), DOWN(0)
KEYCODE_DPAD_DOWN(20), UP(1)

But for the 4 B0 through B3, or A, B, X, Y keys, I have two keycodes for down and two for up :
KEYCODE_BUTTON_A(96), DOWN(0)
KEYCODE_DPAD_CENTER(20), DOWN(0)
KEYCODE_BUTTON_A(96), UP(1)
KEYCODE_DPAD_CENTER(20), UP(1)

KEYCODE_BUTTON_X(99), DOWN(0)
KEYCODE_DEL(67), DOWN(0)
KEYCODE_BUTTON_X(99), UP(1)
KEYCODE_DEL(67), UP(1)

KEYCODE_BUTTON_Y(100), DOWN(0)
KEYCODE_SPACE(62), DOWN(0)
KEYCODE_BUTTON_Y(100), UP(1)
KEYCODE_SPACE(62), UP(1)

KEYCODE_BUTTON_B(97), DOWN(0)
KEYCODE_BACK(4), DOWN(0)
KEYCODE_BUTTON_B(97), UP(1)
KEYCODE_BACK(4), UP(1)

Sometimes - is it a bug of the app or of the gamepad ? -, the line with the second keycode (space, del, dpad_center, back) is repeated twice (2 for down and 2 for up)

I tried to debug my small app by stopping in FormKeyUp since it goes there (and not in FormKeyDown) but I can't seem to find any call where key is <> 0 and keychar <> #0, anywhere between FMX.Platform.Android.TPlatformAndroid.HandleAndroidInputEvent and FormKeyUp, but may be I did not look at the good values.

Anyway, since either down and up movements send two keycodes (and sometimes three it seems), might that be an explanation with Delphi has a problem ?

Eli M

Posts: 1,346
Registered: 11/9/13
Re: Gamepad and Android
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 5, 2016 8:11 PM   in response to: Marc Bonnaud in response to: Marc Bonnaud
In Androidapi.Keycodes.pas I found:

AKEYCODE_BUTTON_A = 96; // $60 A Button key. On a game controller, the A button should be either the button labeled A or the first button on the bottom row of controller buttons.
AKEYCODE_BUTTON_B = 97; // $61 B Button key. On a game controller, the B button should be either the button labeled B or the second button on the bottom row of controller buttons.
AKEYCODE_BUTTON_C = 98; // $62 C Button key. On a game controller, the C button should be either the button labeled C or the third button on the bottom row of controller buttons.
AKEYCODE_BUTTON_X = 99; // $63 X Button key. On a game controller, the X button should be either the button labeled X or the first button on the upper row of controller buttons.
AKEYCODE_BUTTON_Y = 100; // $64 Y Button key. On a game controller, the Y button should be either the button labeled Y or the second button on the upper row of controller buttons.

But in FMX.KeyMapping.pas they are not there. What you should probably do is copy FMX.KeyMapping.pas to your project and then edit it to add something like:

{$IFDEF ANDROID}
{$REGION 'Android key map'}
procedure TKeyMapping.RegisterVirtualKeys;

OtherKeys: array [0..33] of Word = (
AKEYCODE_HOME, vkBrowserHome,
AKEYCODE_BUTTON_A, vkGamePadA,
AKEYCODE_BUTTON_B, vkGamePadB,
AKEYCODE_BUTTON_X, vkGamePadX,
AKEYCODE_BUTTON_Y, vkGamePadY,
Marc Bonnaud

Posts: 14
Registered: 7/1/16
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 6, 2016 3:11 AM   in response to: Eli M in response to: Eli M
Thanks a lot, it works. :)

Of course, it was [0..37] (four lines but eight values) and since no vkGamePadA, vkGamePadB, vkGamePadX, vkGamePadY are in System.UITypes.pas, I made a copy in my project folder and added at the first slot I could find with 4 consecutive unused values : vkGamePadA = $3A; vkGamePadB = $3B; vkGamePadX = $3C; vkGamePadY = $3D;

Of course also, since the bottom button (A) sends both a vkGamePadA and then a vkHardwareBack, the app exits after FormKeyUp, like in most other Android apps.
I can handle this by adding at the end of FormKeyUp : if Key=vkHardwareBack then Key := 0; in order to "fool" TWindowManager.KeyUp in FMX.Platform.Android.pas

With only my four direction button, I had started adding many trackbars in the upperleft corner of the Viewport with feature selection by up/down and change of value by left/right, but now I can devise a much straighter user interface.

Thanks again.
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 6, 2016 10:17 AM   in response to: Marc Bonnaud in response to: Marc Bonnaud
Am 06.10.2016 um 12:11 schrieb Marc Bonnaud:
Thanks a lot, it works. :)

Of course, it was [0..37] (four lines but eight values) and since no vkGamePadA, vkGamePadB, vkGamePadX, vkGamePadY are in System.UITypes.pas, I made a copy in my project folder and added at the first slot I could find with 4 consecutive unused values : vkGamePadA = $3A; vkGamePadB = $3B; vkGamePadX = $3C; vkGamePadY = $3D;

Of course also, since the bottom button (A) sends both a vkGamePadA and then a vkHardwareBack, the app exits after FormKeyUp, like in most other Android apps.
I can handle this by adding at the end of FormKeyUp : if Key=vkHardwareBack then Key := 0; in order to "fool" TWindowManager.KeyUp in FMX.Platform.Android.pas

With only my four direction button, I had started adding many trackbars in the upperleft corner of the Viewport with feature selection by up/down and change of value by left/right, but now I can devise a much straighter user interface.

Thanks again.

Hello,

not so fast ;-)
Since you know now how to make it work and that there's a shortcomming
in FMX or RTL you should go to

quality.embarcadero.com and file a case there.
That makes EMBT aware of it in case they aren't already and if things
work like they should they'll fix that and make your copied and patched
file superflous someday.

And you help all others having the same or similiar problems.

Greetings

Markus
Marc Bonnaud

Posts: 14
Registered: 7/1/16
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2016 5:26 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:
you should go to
quality.embarcadero.com and file a case there.

Ok, I sent it.

By the way, a fifth declaration was missing :

AKEYCODE_DPAD_CENTER, vkCenter,

(and therefore OtherKeys: array [0..39] of Word = ( )

in KeyMapping

and

vkCenter = $3E;

in System.UITypes

to get the two keycodes sent by the A or B0 key (the one with a X cross on my device)

At the end, I suggested a Google VR SDK for FireMonkey soon here :
https://developers.google.com/vr/daydream/overview

and taking soon into account the new remote (with sensors) of Google's brand new headset here :
https://www.youtube.com/watch?v=rLLAA4ENIP4
which was presented last Tuesday at Google's keynote.
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2016 10:02 AM   in response to: Marc Bonnaud in response to: Marc Bonnaud
Am 08.10.2016 um 14:26 schrieb Marc Bonnaud:
Markus Humm wrote:
you should go to
quality.embarcadero.com and file a case there.

Ok, I sent it.

1. if you tell us the report number we might help if it should need
refinement/discussion etc.

2. if a 5th item is missing as well I hope it is not missing from your
report ;-)

Greetings

Markus
Marc Bonnaud

Posts: 14
Registered: 7/1/16
Re: Gamepad and Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2016 10:51 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

1. if you tell us the report number we might help if it should need
refinement/discussion etc.

https://quality.embarcadero.com/browse/RSP-16052

2. if a 5th item is missing as well I hope it is not missing from your
report ;-)

You must be joking :)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02