How can I capture the hardware keyboard events without using an EditText field?
For example, in a simple activity the display a square on the screen, when a "B" is pressed on the slide keyboard I want to turn it blue, when a "G" is presses, turn it Green, etc.
I don't need help with the color code, just how to intercept the keypress
This is not about the soft or virtual keyboard
Android classes usually provide event handlers, you can implement when subclassing them. The Activity class has the following event handlers:
onKeyDown(int keyCode, KeyEvent event)
onKeyLongPress(int keyCode, KeyEvent event)
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
onKeyShortcut(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
In addition all views have the following event handlers:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
I guess there are many other classes that have similar event handlers for key events, but this should be enough for your situation. The KeyEvent then contains information about the pressed key, i.e. the key code.
Activity class has already implemented KeyEvent.CallBack see here
you just need to override these methods and implements all events that you want
Related
I'm trying to detect when the soft keyboard gets closed, I found this code snippet
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//detects that keyboard was hidden
}
return super.onKeyPreIme(keyCode, event);
}
but this needs to be added to a subclass that extends editText, I'm not intrested in implementing the functionality I want to add on keyboard hidden in all my app, I want to add it for just one Activity, I tried onKeyDown, onKeyBack, onKeyUp, onBackPressed nothing seems to log the back press that closes the soft keyboard.
so my question is there a way to detect the click on the button that hides the keybaord?
I recently discovered that I could connect my wireless keyboard and mouse with my phone using an OTG adapter. My phone now has an always available, physical keyboard as well as a cursor.
I was curious if I could reroute the keyboard input to preform different manual touch events when not being used for actual typing. For example, I want various keys to trigger a manual click in the appropriate areas to reload, crouch, shoot, move, etc. in PUBG mobile.
In an active activity, I can capture a keyboard event as follows:
public class MainActivity implements KeyEvent.Callback {
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) { ... }
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) { ... }
#Override
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) { ... }
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) { ... }
}
I am unfamiliar with background operations on Android, but the developer page over viewing background services explicitly states that:
It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
Both these limitations prevent a background service from being a viable option for my keyboard trick, not to mention I have no clue how to capture keyboard events in these services.
What other options do I have?
Also, would I get banned from PUBG mobile for successfully setting up such a thing?
I have a EditText that should not allow the user to input anything through the keyboard (soft or hard). This EditText should only allow the user to input something through keys(buttons) displayed in the screen by the app.
I have disabled the soft keyboard, but I can't find a way to disable the hardware keyboard input. This input via hardware keyboard can be done using a emulator that is configured to allow input through the hardware keyboard.
So, my question is, How can I block the input via physical keyboard in a EditText?
Thank you!
Finally got it!
Code:
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
Explanation:
Returning true will tell the listener that I've already handled the hardware keyboard input. Based on Android documentation (http://developer.android.com/reference/android/view/View.OnKeyListener.html)
View.OnKeyListener: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.
onKey(View v, int keyCode, KeyEvent event)
Called when a hardware key is dispatched to a view.
You could try these according to what you want in your xml layout
android:longClickable="false"
android:clickable="false"
android:cursorVisible="false"
android:editable="false"
Then make the edittext show what the user typed through the keyboard you made in the app.
I know that onTouchEvent()
can return the current touch point's x,y in a view,
However, when I touch a key on a softkeyboard.
the method is not working,
My question is how can I apply onTouchEvent to softkeyboard on Android?
It is not possible to get a touch event for a soft keyboard.
If you want to capture keys as they are entered into an EditText, check out the EditText.addTextChangedListener() function.
Alternatively, try implementing the View.OnKeyListener interface in your Activity, or overriding public boolean dispatchKeyEvent(KeyEvent e), or overriding public boolean onKeyDown(int keyCode, KeyEvent event). In all of those cases, you can check what the keyCode or KeyEvent is and handle it as you wish.
How can I disable a tab key on a form so that it goes to the next button when press.
This is a hardware Tab key on a qwerty hardware keyboard.
You should implement the protected boolean onKeyDown(int keyCode, KeyEvent event) {}, check for the keyCode and if the key is tab, then consume the event.
Hope this helps!