Codename One - scanning barcodes checking for new line/ENTER character - android

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));
}
}
}

Related

Catch keyboard input from barcode scanner

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

How to Disable Home Button on Android TV Remote

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

How to check whether the input is given by barcode scanner or by soft keyboard?

I wanted to check programmatically that whether the input given by user is either from soft keyboard or the barcode scanner attached to the android tablet.
There is no edit text in activity where I want to apply this so please avoid providing solutions that are applicable through EditText.
With dispatchKeyEvent you can listen for any KeyEvent from the barcode scanner.
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
// do something on input
return false; // prevent default behaviour
}
Use following code to read input/values from barcode scanner, so onKeyDown you need to override in your activity/dialog
String barcode="";
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
log("Key Down keyCode " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {//if scanner doesn't return enter key code then make sure that any view must not have focus in window
//write your code to process the scanned barcode input
barcode = "";
} else {
Character input = (char) event.getUnicodeChar();
log("Scanner Input " + input);
if (Character.isDigit(input) || Character.isLetter(input)) {
barcode += input;//concat the characters
}
}
return true;
}

Device (=keyboard) is sending ASCII (alt+number) to Android

I have external bluetooth device, which is basically something like a keyboard.
But instead of letters and numbers this keyboard is sending data in ASCII.
instead of "0" it will send "alt+48"
instead of "M" it will send "alt+77"
And since Android doesn't have ALT he is is ignoring all received data (=ALT is pressed while receiving letters = I will ignore these letters).
Do you have any idea how to filter out pressed ALT? Or make Android accept send letters with pressed ALT?
override dispatchKeyEvent(KeyEvent event)
and use action KeyAction.ACTION_DOWN else you'll get duplicate data
and then set that add that character in your edit text
edittext.setText(edittext.getText().toString+(char)
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN) {
int c = event.getUnicodeChar();
if (IS_NEW_RF_ID) {
rfIdAcTv.setText("");
}
}
return super.dispatchKeyEvent(event);
}

Actionscript 3: Controlling the physical back button in Android

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;
}

Categories

Resources