I'm new in android and want to write simple detect key press in edit box ,for that purpose write this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(this, Integer.toString(event.getKeyCode()), Toast.LENGTH_SHORT).show();
return super.dispatchKeyEvent(event);
};
but that code just get the enter key code,and dont get code for example a key ,what happen?How can i solve that problem?
Related
I am creating a qml app for android and run into the following problem when trying to catch button presses:
When I press the volume up, volume down or the home button. The following snipped prints: 0 which means the key event is unknown (Based on the QKeyEvent docu)
Keys.onPressed: {
console.log(event.key);
}
I also see this warning in logcat when pressing the home button:
Unhandled key code 3 !
or this warning when pressing the volume up button:
Unhandled key code 252 !
My Question: How can I add a handler/mapper for this key codes ?
Example I want to map the key code 3 to Qt.Key_Home in order to receive the correct key event in Keys.onPressed
I was able to 'remap' the key codes my devices uses to the key codes Qt expects by:
Copy $QT/src/android/java/src/org/qtproject/qt5/android/binding/ into your proejct folder.
Change ANDROID_PACKAGE_SOURCE_DIR in your .pro to point to the this folder.
rename the package name of all the classes in your new android/binding/ folder to fit the package name of your qml app.
add your custom mapping code in onKeyDown():
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//do the remapping for the home button
if(keyCode == 3) {
keyCode = 122;
}
if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null)
return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyDown, keyCode, event);
else
return super.onKeyDown(keyCode, event);
}
Not near my QML machine but of top of my head do something like this.
Create a global object with required key codes like..
QtObject {
id: key_code
property int volume_up: 252
property int volume_down: <key_code_val>
property int key_home: 3
}
Then use it like..
Keys.onPressed {
if (event.key === key_code.volume_up) {
//Do volume up handling..
event.accepted = true
}
}
Also take a look at this: http://doc.qt.io/qt-5/qt.html#Key-enum
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
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 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);
}
I want to pop up the software keyboard when the user presses the search hardware search key.
At the moment I use the following function with doesn't seem to work for the search key but which does work for the back key:
The logging doesn't get even tiggered through the search key.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
Log.v(TAG, "On back key press");
renderPrevious();
return false;
}
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Log.v(TAG, "On search key press");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
return true;
}
I have no text field but want to handle the input directly myself if that matters.
this method setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) doesnt wakeup the keyboard. Check this link. More to know about it check this example. hope it helps
Edit:
try this showSoftInput method
Maybe this would be helpful:
InputMethodManager inputMgr = (InputMethodManager)getSystemService Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
http://plainoldstan.blogspot.com/2010/09/android-set-focus-and-show-soft.html
"When experimenting I was not actually getting what I wanted until I realized I should have an emulator device with no hardware keyboard:"