Android usb mouse right click correct behaviour (I

2019-02-13 17:11发布

Is right click of a USB mouse plugged into android (ics) always designated as the 'back' button? I have one tablet where it isn't.

I want to remove the status bar, and still be able to use the tablet with USB mouse.

Does anyone know where/how to configure the behavior in android?

1条回答
做自己的国王
2楼-- · 2019-02-13 18:04

Late answer, but the answer is yes. I am going to provide some more detail on this topic as it is the only topic on the Internet of this type.

Default behavior: Android treats this as a back button, with no visible exceptions.

This is a pretty nice feature for phones and normal apps. However, two types of people would like this feature to be implemented differently. Modders, and app developers.

Modders' Fix: On a rooted device (and potentially unrooted for quite a few underdog devices with unlocked adb), observe Generic.kl under /system/usr/keylayout. Search for the flag KEY_BACK, there may be multiple keycodes on multiple lines linked to it. For me it was key 158 with the WAKE_DROPPED flag. For a generic "right-click" function you'll want to swap the number codes for the back key and the menu key, so when the mouse sends a back-key command the system will actually fire a menu key command, while maintaining some hardware key (labeled menu key) on the device for going back. Yes, this will reverse the hardware keys on your phone, but it is the easiest solution, without rebuilding the entire ROM. If this is for a corporate or professional cause, maybe see this article: Overriding Mouse Events Kernel-Level. I would actually recommend creating an Xposed module that overrides the procedure they referenced. Otherwise, look up how to remap keycodes, sorry I could not find a straight-forward article here.

App-developers' Fix: Non-root: Override back key in your application and return true. In your onBackPressed() override, perform any right-click function necessary. Simple! Since you're most likely wanting a PC-like context menu functionality, you'll also need to track where the mouse is at, via a MotionEvent listener applied to the root view with an event.getRawX() and RawY() calls storing values to a global variable. You can then place your context menu (sized ListView or VerticalLinearLayout) under the cursor by setting top and left margins and set it visible. If you want to perform a secondary function on a hovered object in your layout via right-click, such as delete or copy, you will have to manually figure out what object the stored mouse coordinates are hovering over. You'll probably have to add MotionEvent.ACTION_HOVER_ENTER listeners on every affected object and store the object ID in a global variable for use when the onBackPressed() function is called. (I will provide code when I have time!)

Hope this helps anyone exploring such a specific topic! :)

查看更多
登录 后发表回答