• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Possible to Intercept keyboard key presses?

tannim

Newbie
I had an idea for an application, but I don't know if it's possible or not.

Can a program sit in the background and intercept keypresses, convert the keypresses from one letter to another, and send it to whatever program you are working in as though the new letter is what you typed?

As qwerty is the only layout choice for hardware keyboards and I don't have root on my tablet, I want to use this as a work around.

If I were to press "s" on my hardware usb keyboard. The program would intercept the "s" before it got to whatever textbox and convert it into the dvorak key equivalent of "o". That o would then be passed to the program as a normal keypress.

if I were to press asdfg on the qwerty aoeui would be sent instead.

Is this possible to do? I thought it best to ask before I waste hours trying to learn how to write the program.

List of changes I would make in a pseudocode case statement format.
Select Case Key
#Comment - Number Row Key changes
- = change to [
= = change to ]
#Comment - Top Row Key changes
q = change to '
w = change to ,
e = change to .
r = change to p
t = change to y
y = change to f
u = change to g
i = change to c
o = change to r
p = change to l
[ = change to /
] = change to =
#Comment - Middle Row Key changes
#Comment - A = A Same in Dvorak as Qwerty Handled by the Else
s = change to o
d = change to e
f = change to u
g = change to i
h = change to d
j = change to h
k = change to t
l = change to n
; = change to s
' = change to -
#Comment - Bottom Row Key changes
z = change to ;
x = change to q
c = change to j
v = change to k
b = change to x
n = change to b
#Comment - M = M Same in Dvorak as Qwerty Handled by the Else
, = change to w
. = change to v
/ = change to z
else
#Comment - For keys that are the same in Dvorak as in Qwerty
pressed key = pressed key
end case
 
You can program full keyboard replacements and should in theory be able to handle hardware keyboards with that same replacement.

However you can't "sit in the background" and intercept. The user would be fully aware and have to go through the process of choosing your keyboard.

This might help:
InputMethodManager | Android Developers

Creating an Input Method | Android Developers

Intercepting hard key events

Even though the input method window doesn't have explicit focus, it receives hard key events first and can choose to consume them or forward them along to the application. For instance, you may want to consume the directional keys to navigate within your UI for candidate selection during composition. Or you may want to trap the back key to dismiss any popups originating from the input method window. To intercept hard keys, override InputMethodService.onKeyDown() and InputMethodService.onKeyUp(). Remember to call super.onKey* if you don't want to consume a certain key yourself.
 
Back
Top Bottom