I want to create keyboard shortcuts in my Android app such that, say, alt-t runs a certain command. I can't seem to figure out how to detect the presence of the alt modifier in the emulator, though.
I've overridden onKeyDown() in my app to look like the following (Scala):
override def onKeyDown(keycode:Int, event:KeyEvent) = keycode match {
case KeyEvent.KEYCODE_B =>
StatisticsService.map(_.sayBatteryLevel())
true
case KeyEvent.KEYCODE_D =>
StatisticsService.map(_.sayDate())
true
case KeyEvent.KEYCODE_S =>
StatisticsService.map(_.saySignalStrengths())
true
case KeyEvent.KEYCODE_T =>
StatisticsService.map(_.sayTime())
true
case _ => super.onKeyDown(keycode, event)
}
That of course matches the plain keys just fine, but not alt-b, alt-t, etc. How can I change the above to match the given alt-modified bindings?
I've searched Google and have tried using event.isAltPressed, but this doesn't work. I've also logged the results of the keypresses, and have noticed that the alt key isn't at all picked up. That is, simply pressing alt does nothing, and pressing alt-t produces identical logs to just t.
This is being tested in the emulator if that makes a difference.
Edit: Not sure how to respond to comments left on my question, but I'm aware that not all devices have alt keys, but that's not an issue here because I'm coding for a custom device that does. The question isn't "what's the most generic way to do this," but "how do I match alt keybindings when the documented methods don't work and no explanation seems to be given in Google's own docs?"
Thanks.
Related
Android supports external hardware keyboards as input, which may send keyboard shortcuts (e.g. Ctrl+A) in which there are some ways to program my app to accept shortcuts from external keyboards (e.g. like this: Custom keyboard shortcuts).
I came across this keyboard shortcut "help" screen in Android (activated by pressing OS + /) (see picture below)
and noticed some apps have some keyboard shortcuts directly registered within the system shortcuts help screen(these screens are not provided by the app, they can be found by pressing OS + /).
So far, the apps that I see that have this feature are:
Samsung Internet
Samsung One UI Home
Google Chrome
Chromium-based browsers (e.g. Brave)
Google Docs, Sheets, Slides
I cannot find any way to register my keyboard shortcuts in my app programmatically such that they will be recognised by the system. How can I possibly implement it in my app?
I do know iOS/iPadOS has such a feature.
Perhaps Android does have such a feature too? It seems like it would be quite useful to app developers and users, not sure why it's not documented at all in Android Developer documentation.
Thanks.
This feature is not documented in the Android Developer website, so I had to search around.
Since it is supported by AOSP, this is not a proprietary method/function by Google. No root is required on your users' devices.
Since this feature is available in Chromium-based browsers, I decided to check up the open-source Chromium Android source code (a mirror on GitHub is available here by someone: https://github.com/kuoruan/Chromium-Android) and I have discovered how this feature is implemented.
The relevant lines are here:
https://github.com/kuoruan/Chromium-Android/blob/29ba2966ff145c9cb2492a971f9c03f879c5b9c9/app/src/main/java/org/chromium/chrome/browser/KeyboardShortcuts.java#L117
https://github.com/kuoruan/Chromium-Android/blob/29ba2966ff145c9cb2492a971f9c03f879c5b9c9/app/src/main/java/org/chromium/chrome/browser/ChromeTabbedActivity.java#L2135
In any Activity, you can override the method onProvideKeyboardShortcuts.It provides 3 parameters: data, menu, and deviceId, in which we only need data here.
Here is the method signature:
fun onProvideKeyboardShortcuts(
data: MutableList<KeyboardShortcutGroup>?,
menu: Menu?,
deviceId: Int
)
This only works on API 24 (Android Nougat) and above.
data is a mutable list of KeyboardShortcutGroup, which is a group of keyboard shortcuts.
So, you can have multiple groups containing multiple shortcuts, useful for grouping the shortcuts for your users.
To register your shortcuts,
Create a KeyboardShortcutGroup. This is required to store and group your keyboard shortcuts together. The class accepts a CharSequence as a parameter for the name of the group, so you have to use getString(R.string.your_resource) for string resources.
val keyboardShortcutGroup = KeyboardShortcutGroup(/* the name of your group here: */ "Test Group")
Define your keyboard shortcuts. Keyboard shortcuts are defined using KeyboardShortcutInfo objects, which accepts 3 parameters in it's constructor - a label, the key(e.g. A/ Z/ 8/ 9), and the modifier key. A single keyboard shortcut is created this way:
KeyboardShortcutInfo("Shortcut One" /*label*/, KeyEvent.KEYCODE_Z/*key*/, KeyEvent.META_ALT_ON/*modifier*/)
You need to pass in key codes into the constructor for the key and the modifier key.
The key and modifier key must be passed in seperately.
Here are the list of available key codes:
https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_0 (See those prefixed with KEYCODE)
Based on testing, the modifier keys which work are:
Ctrl META_CTRL_ON
Alt META_ALT_ON
Shift META_SHIFT_ON
Super/OS/Meta (see https://android.stackexchange.com/a/243218/307843) META_META_ON
Function META_FUNCTION_ON
Left/Right keys for all of these are also available(i.e. META_ALT_LEFT_ON)
META_SYM_ON may work(have not tested yet), but Caps Lock doesn't work.
To combine modifier keys,
in Java, use the bitwise operator:
KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_CTRL_ON
in Kotlin, use the or operator:
KeyEvent.META_ALT_LEFT_ON or KeyEvent.META_CTRL_ON
To this KeyboardShortcutGroup, add shortcuts using addItem.
keyboardShortcutGroup.addItem(shortcut /* <-- the shortcut created earlier */)
Add your KeyboardShortcutGroup to data
data.add(keyboardShortcutGroup)
Your code should look like this:
override fun onProvideKeyboardShortcuts(
data: MutableList<KeyboardShortcutGroup>?,
menu: Menu?,
deviceId: Int
) {
super.onProvideKeyboardShortcuts(data, menu, deviceId)
// Requires API 24
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val keyboardShortcutGroup = KeyboardShortcutGroup("Test Group")
keyboardShortcutGroup.addItem(KeyboardShortcutInfo("Shortcut One", KeyEvent.KEYCODE_Z, KeyEvent.META_ALT_ON))
data?.add(keyboardShortcutGroup)
}
}
You should see the end result:
You can use this to inform users of available shortcuts. Note that this does not implement them, it merely adds a listing to the Android shortcut menu.
Looks silly, but I can't get meta state out of KeyEvent, accessed from onKeyListener. Tried with all keyboards I have and with emulators.
Whether or not Shift, Ctrl, etc keys are pressed, keyEvent.getMetaState() returns 0. May it works for TextListener, but I don't need it for entering text, I just want to differentiate between Tab and Shift+Tab.
Thanks for anticipated help.
Update. What I just figured out is that meta state is reported for alphabetic keys, but not for other keys.
For example if I press left Shift+T the system generates KeyEvent for KEYCODE_SHIFT_LEFT and KEYCODE_T, and KeyEvent for KEYCODE_T has META_SHIFT_ON set. You can trick the system with Shift+TAB+T, in which case META_SHIFT_ON is set for both KEYCODE_T and KEYCODE_TAB. However with Shift+TAB the KeyEvent for KEYCODE_SHIFT_LEFT is not generated, and the meta state remains unaffected.
Maybe system keyboard configuration files need to be updated to allow combinations like Shift+TAB?
Yes, it's really about android configuration file.
You modify TAB entry in /system/usr/kychars/Generic.kcm (or the file for corresponding vendor) and add a line for shift, as the following:
key TAB {
label: '\t'
base: '\t'
shift: '\t'
ctrl, alt, meta: none
}
Some devices uses qwerty.kcm, which has the shift line alaready present. They should work OK without intrusion. BTW, unicode has provides a special code \u21B9 for Shift+TAB, but it might not be recognised by Android.
I have a small phonegap application with jquery mobile and backbone.
I'm trying to show popup to user by manually calling .popup() method.
Everything works fine on iOS but on android I got strange issue: popup is showing for few moments and than disappear.
Here the actual code:
var PostView = Backbone.View.extend({
events: {
'touchend .add-comment-button': 'addComment'
},
addComment: function() {
this.$(".comment-popup").popup('open', { history: false });
return false; // Stop bubbling.
}
});
I'm using history: false because this popup is actualy part of subpage.
The code looks very simple, I'm just can't understand why it can disappear, and why this happen only on android devices.
Thanks, and sorry for my bad english.
I spent hours trying to fix this problem.
Finally I ended up doing the following two things that seemed to fix the problem.
1 - Use the uncompressed jqm file. i.e jquery.mobile.1.2.0.js
2 - I was triggering the popup programatically using the 'tap' option - once changed to the 'click' option it worked.
$('.option').live('click', function() {
$('#popup-div').popup('open');
});
I spent hours trying to fix this problem.
Finally I ended up doing the following two things that seemed to fix the problem.
this code snippet may help you ->
$('#testBtn').on('tap',function(e){
console.log("button clicked");
e.preventDefault();
$('#testPOPUP').popup("open");
});
Please note i have used e.perventDefault().
I didn't feel like changing my .tap() events to the click event and I didn't have a case where I could use preventDefault()so I just added a timeout to the popup('open') line. My hoverdelay in jqm is set to 150 so I set this timeout to 600 just to be on the safe side. Works fine, doesn't feel sluggish for the user.
One way to 'fix' it is by setting data-history="false" on the popup div
See also this question
JQuery Mobile popup with history=false autocloses
I have the exact same problem when trying to use popup('open') on an android 2.3 device (both in native browser and in firefox) and it works just fine on browsers on other devices. I'm also using backbone event management to open my popup (used the tap event and no aditionnal options to popup).
What I did to 'correct' the problem is that I removed the backbone event management for this event and added a listener in the render function. In your case this would look something like this :
events: {
// 'touchend .add-comment-button': 'addComment'
},
render: function() {
$(this.el).html(this.template(this.model));
$(this.el).find('.add-comment-button').tap(function(el){
this.addComment(el);
return false;
}.bind(this));
}
I have no idea where the problem comes from (must be some incompatibility between backbone and jquery mobile) and why we only see it on android but for the moment with this workaround my app seems to work fine on any device.
Edit: oops, it turns out that in my case the problem was I was missing "return false;" in the function dealing with the event.
Now that I added it, it works correctly with the backbone event management.
Sadly that doesn't explain why you have the issue and why I was seeing it only on android.
In case it helps anyone, I had the same problem occurring with Bing Maps, with the Microsoft.Maps.Events.addHandler(pin, 'click', callback) method.
Not particularly nice, but instead I stored an ID in pushpin._id and did the following:
$("#page").on('vclick', function (event) {
if (event.target.parentElement.className === "MapPushpinBase") {
$("#stopPopup").popup('open');
}
});
One brute force option is to check whether popup was hidden and reopen it.
In a loop, because the exact time the popup becomes hidden seems to be varied.
var hidden = $('#' + id + '-popup') .hasClass ('ui-popup-hidden')
if (hidden) $('#' + id) .popup ('open')
A working example: http://jsfiddle.net/ArtemGr/hgbdv9s7/
Another option could be to bind to popupafterclose:
var reopener = function() {$('#' + id) .popup ('open')}
$('#' + id) .on ('popupafterclose', reopener)
$('#' + id) .popup ('open')
Like here: http://jsfiddle.net/ArtemGr/gmpczrdm/
But for some reason the popupafterclose binding fails to fire on iPhone 4 half of the time.
I know that in PhoneGap there's a way to do this, but can it be done for an HTML5 web app? I'd like to have Android users be able to use the back button within the webapp to provide a consistent UX, but of course the default is to go back in the browser history and leave the app...
Edit: tried, didn't do anything on any button press on a Google Nexus S:
document.onkeydown = checkKeycode;
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
alert("keycode: " + keycode);
}
Edit again: The ultimate answer seems to be to create history points at each UX interaction -- using URL hashes like #!/main/about_us in the URL. This then allows for back-button use, so long as you make sure that the UI triggers a history.back() when a UI back button is tapped.
The ultimate answer seems to be to create history points at each UX interaction -- using URL hashes like #!/main/about_us in the URL. This then allows for back-button use, so long as you make sure that the UI triggers a history.back() when a UI back button is tapped.
Override the OnKeyDown Event in your app and look for KEYCODE_BACK.
If you are handling the event then return true else false.
I'm building a mobile web app targeting Android users. I need to know what DOM events are available to me. I have been able to make the following work, but not terribly reliably:
click
mouseover
mousedown
mouseup
change
I have not been able to get the following to work:
keypress
keydown
keyup
Does anyone know the full list of what is supported and in what contexts (e.g., is onchange only available to form inputs?)? I can't find a reference for this on The Googles.
Thanks!
Update: I asked the same question on the Android developers list. I will be doing some more testing and will post my results both here and there.
OK, this is interesting. My use case is that I have a series of links (A tags) on a screen in a WebKit view. To test what events area available, using jQuery 1.3.1, I attached every event listed on this page (even ones that don't make sense) to the links then used the up, down, and enter controls on the Android emulator and noted which events fired in which circumstances.
Here is the code I used to attach the events, with results to follow. Note, I'm using "live" event binding because for my application, the A tags are inserted dynamically.
$.each([
'blur',
'change',
'click',
'contextmenu',
'copy',
'cut',
'dblclick',
'error',
'focus',
'keydown',
'keypress',
'keyup',
'mousedown',
'mousemove',
'mouseout',
'mouseover',
'mouseup',
'mousewheel',
'paste',
'reset',
'resize',
'scroll',
'select',
'submit',
// W3C events
'DOMActivate',
'DOMAttrModified',
'DOMCharacterDataModified',
'DOMFocusIn',
'DOMFocusOut',
'DOMMouseScroll',
'DOMNodeInserted',
'DOMNodeRemoved',
'DOMSubtreeModified',
'textInput',
// Microsoft events
'activate',
'beforecopy',
'beforecut',
'beforepaste',
'deactivate',
'focusin',
'focusout',
'hashchange',
'mouseenter',
'mouseleave'
], function () {
$('a').live(this, function (evt) {
alert(evt.type);
});
});
Here's how it shook out:
On first page load with nothing highlighted (no ugly orange selection box around any item), using down button to select the first item, the following events fired (in order): mouseover, mouseenter, mousemove, DOMFocusIn
With an item selected, moving to the next item using the down button, the following events fired (in order): mouseout, mouseover, mousemove, DOMFocusOut, DOMFocusIn
With an item selected, clicking the "enter" button, the following events fired (in order): mousemove, mousedown, DOMFocusOut, mouseup, click, DOMActivate
This strikes me as a bunch of random garbage. And, who's that cheeky IE-only event (mouseenter) making a cameo, then taking the rest of the day off? Oh well, at least now I know what events to watch for.
It would be great if others want to take my test code and do a more thorough run through, perhaps using form elements, images, etc.
Since this is the second most popular Android + JavaScript post on SO (which is just a sad commentary on the state of web development targeting the Android platform), I thought it may be worthwhile including a link to pkk's touch event test results at http://www.quirksmode.org/mobile/tableTouch.html and also http://www.quirksmode.org/mobile/ in general.
As of Android 1.5, the same touch(start|move|end|cancel) events that the iPhone supports work in Android as well.
One problem I found was that touchmove ends get queued up. No workaround yet.