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
Related
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;
}
};
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
in Android I can easily detect the ENTER character using the setOnEditorActionListener method so that when a barcode is scanned into a textfield I know when the whole barcode has been read.
[Here is the Android code as requested:]
scanEditText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN )))
{
System.out.println(" *** ENTER!");
return true;
}
else
return false;
}
});
I have been trying to replicate this in Codename One but with no luck. I have tried
setDoneListener
addDataChangedListener
ActionEvent on the text field and text area
overriding the keyPressed and keyReleased methods
but with no joy. If I set a textField with setSingleLineTextArea(false) I can see the barcode characters appear in the box followed by what I am assuming is a
new line character as the next scan appears on a new line. None of the methods listed seem to catch this event. If I change the focus to another box the ActionEvent is triggered but I don't want to have to move the focus each time.
[Here is the Codenameone code]
gui_searchTextField.setSingleLineTextArea(false);
gui_searchTextField.setDoneListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Dialog.show("KB", "doneListener " +
gui_searchTextField.getText().trim(), "OK", null);
}
});
gui_searchTextField.addDataChangedListener((evt1, evt2) ->
{
System.out.println("DataChanged " + gui_searchTextField.getText());
});
So, how do I catch the end of the barcode like I can easily in the Android SDK?
Text input is very much a special case for us in all OS's so the keys go directly into the native OS text editing and don't "travel" through Codename One API's. However, DataChangeListener should be invoked for every key input so I'm not sure how this is happening exactly.
I did notice you are using System.out instead of Log.p so you might miss the output from the device related to the changes. Also I would recommend overriding the key pressed/released events and not starting the text editing. Then adding the key value to the text field instead of letting the native input handle it e.g.:
class MyForm extends Form {
public void keyReleased(int key) {
if(isCrLf(key)) {
// do this...
} else {
myTextField.setText(myTextField.getText() + ((char)key));
}
}
}
I'm working on an input device that has a button that I'd like to act both as a home button when pressed by itself, but also work as a modifier for the other buttons on the device.
I know you can do this in the kcm file with other buttons, and have something like:
key ESCAPE {
base: fallback BACK
alt, meta: fallback HOME
ctrl: fallback MENU
}
Is it possible to have something like:
key CTRL_LEFT {
base: fallback HOME
{SOMETHING HERE}: CTRL_LEFT
}
so that I can press that button with another button and have it not fallback on HOME?
Thanks in advance! :D
This functionality is not possible on a system level: see
http://source.android.com/tech/input/key-character-map-files.html for a list of all options possible.
However, if you would like to have it on an application level, you could program it in as follows:
import android.view.KeyEvent;
private boolean home = false;
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29) //29 == CTRL_LEFT
home = true; //if we release this button, HOME needs to be invoked
else home = false; //so CTRL_LEFT was used as a modifier: no need to invoke HOME
//allow the system to pass key handling to the next listener
return super.onKeyUp(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29 && home == true)
{
super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME));
return true; //so we absorb the event
}
return super.onKeyUp(keyCode, event);
}
Then, as long as this application is set up to receive the keystrokes (usually a privacy issue unless you're coding it in for yourself), it can process the keystrokes and dispatch the HOME button when need be.
Should you decide to proceed thus, you'd have to remove the
base: fallback HOME
from your code.
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;
}`