I need to use a gamepad (physical joystick and button) to opeate my android application. I check the related offical documentation:enter link description here. and my code: but no toast when I connect and operate the gamepad. Do I need other related libraries? I didn't make any set-up action, nor did we set the manifest.
I found that most joystick related examples are virtual joystick on the phone, but in my case, I am connecting a physical gamepad to my android application.
private View.OnKeyListener mCorkyListener = new View.OnKeyListener() {
public boolean onKey(View v, int i, KeyEvent event) {
// do something when the button is clicked
Toast.makeText(getApplicationContext(), event.getKeyCode() ,Toast.LENGTH_SHORT).show();
switch (event.getAction()){
case KeyEvent.ACTION_UP:
break;
case KeyEvent.ACTION_DOWN:
break;
}
return true;
}
};
Related
I'm building a basic price checker app that scans a barcode and displays information for the product and am trying to run it on an android-powered tablet that comes with a built-in barcode scanner.
The scanner works and if I put a textbox on the app and focus to it, the barcode I scan gets written onto it just fine - however I have been unable to catch the input without having the app focus on a textbox (the app should have no input areas, only images and textview labels).
The scanner shows up as an HID keyboard on the input android settings.
Almsot all the posts I find here are about using the camera to scan barcodes (built my original prototype using this but performance was subpar). One old post here gave me a hint about overriding the dispatchKeyEvent as so
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getCharacters() != null && !event.getCharacters().isEmpty()) {
isRunning = true;
Log.d(TAG, "Starting");
String barcode = event.getCharacters();
new myImageTask().execute(barcode);
}
return super.dispatchKeyEvent(event);
}
However it doesn't seem to be catching any input.
I looked at overriding KeyUp and KeyDown events but they seem to be explicitly built for catching single key events.
Is there another event I could use to catch and read the scanner's full input or should I just chain the KeyDown event to buffer each individual key into a static variable and, after receiving a special input termination character and run my task on the result?
barcodeEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
saveToDBMethod();
barcodeEditText.setText("");
barcodeEditText.requestFocus();
return true;
default:
break;
}
}
return false;
}
});
It may be a hardware configuration trouble. In my case, using Honeywell Android devices with barcode scans, I always have to go to the scan settings and set in barcode reading options the wedge method to keyboard
Edit: KeyEvent.getCharacters() was deprecated in Android 29, so I am not sure the below is a viable long-term solution.
I know this was asked a few years ago, but I am able to capture barcode scans from both a Honeywell and Zebra devices, running Android 11 and 10, respectively, by overriding onKeyMultiple in MainActivity:
#Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
System.out.println("Key Multiple event: " + event.getCharacters());
return super.onKeyMultiple(keyCode, repeatCount, event);
}
Even when I don't have an input in focus, the above fires when I perform a scan. Output:
Key Multiple event: some-barcode
There are a couple of SO posts that have already discussed similar issues, but I find them not much relevant or too complex. I am working with an Android TV which has a remote controller, and it is supposed to be used in an enterprise environment. Users should not be able to have much control, so I have to restrict some of the functions available on the remote.
I created a simple app that overrides onKeyDown() and displays pressed keys using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Read the input and display its code
switch(keyCode) {
case KeyEvent.KEYCODE_HOME:
mTextView.setText("Home");
break;
default:
char c = event.getDisplayLabel();
String code = String.valueOf(keyCode);
String displayText = c + " " + keyCode;
mTextView.setText(displayText);
mTextView.setBackgroundColor(mColor^=Color.GREEN);
}
return true;
}
I am able to capture most of the keys and override their behaviors most notably 131, 132, 133, and 134 (used as Media, TV, Web, and App shortcuts respectively on the remote).
The only problem is the Home button which AOSP source code (KeyEvent.java) says is system-specific:
* This key is handled by the framework and is never delivered to applications. */
public static final int KEYCODE_HOME = 3;
A solution that struck me was to extend KeyEvent and override the method isSystem() to return false when the selected key is KEYCODE_HOME. But, this method is defined final and I cannot override it. Any ideas?
you need to use
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
in your manifest and
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(this, ""+event.getKeyCode(), Toast.LENGTH_SHORT).show();
return false;
return super.dispatchKeyEvent(event);
}
i think control home key is 3
i hope was helpful
I'm developing a simple app for Android using ActionScript 3.0 in Flash CS5 and I'd like to know if there's a way to map the physical back button of the Android's phone to tell my animation to go to the first frame.
I've red that post: Disabling the phone's back button (AIR for Android /ActionScript 3)
so maybe it is possible ? If yes HOW.
Thank you !
//first register key down listener
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
//then listen to the back key
private function handleKeys(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK ) {
logDebug("=>handleKeys.");
NativeApplication.nativeApplication.exit();
}
}
Have you tried overriding onKeyUp method?
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do your stuff
}
return false;
}
I'm trying to handle value from a barcode scanner USB via my Android 3.2 tablet, the scanner is succefuly working in the OS but i want to get the value in the program without a edittext, usbmanager host and accessory did not list it with the connected devices via USB.
most plug in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically. I think this is why they do not show up in the USB host APIs list of accessories. You should be able to get raw input from them the same way you would a keyboard inside your Activity by overriding Activity.onKeyDown(int keycode, KeyEvent ke)
Something like this in your activity:
#Override
protected boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("TAG", ""+ keyCode);
//I think you'll have to manually check for the digits and do what you want with them.
//Perhaps store them in a String until an Enter event comes in (barcode scanners i've used can be configured to send an enter keystroke after the code)
return true;
}
you will get the result on Activity keydown event.
For Example:-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
char pressedKey = (char) event.getUnicodeChar();
Barcode += "" + pressedKey;
Toast.makeText(getApplicationContext(), "barcode--->>>" + Barcode, 1)
.show();
return true;
}
Hope this post will help you.
I also had same problem,but when i used onKeyDown or onKeyUp,it was not called every time i mean for every character for barcode.I Used DiapatchKeyEvent,and it worked nice.
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
Log.i("TAG", event.getCharacters());
return true;
}`
I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}