Throwing/Simulating KeyStrokes programatically

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Throwing/Simulating KeyStrokes programatically

Postby newranji » Fri Jan 30, 2009 1:21 pm

Is there any possibility to throw the key strokes from background service to any running application?

Is there any chance to throw the touch event like this?
newranji
Freshman
Freshman
 
Posts: 3
Joined: Fri Nov 23, 2007 5:59 am
Location: India

Top

Postby guian » Wed Jun 10, 2009 10:32 am

mosquitofs wrote:I found a way to start an button on a very easy method... I just Overrided the method onKeyDown and return false

and then did for example this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 KeyEvent kev = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_6);
  2.                 onKeyDown(KeyEvent.KEYCODE_6, kev);    
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4




I was able to use it to simulate the BACK button pressed. ( KEYCODE_BACK ) on the android Cupcake.
I expected it to hide the virtual keyboard if this last was shown...
unfortunatly it doesn't...


I use the back button to hise one of m personal view, if my view and the virtual keyboard are shown. when I press once the back button => it hides the keyboard. the second press will hide my personal view as expected.

but : if both are shown, and I use this trick to simulate the back Button => it only hide my personal view and let the virtual keyboard shown...

do you know why ?
Last edited by guian on Mon Apr 26, 2010 9:40 am, edited 1 time in total.
guian
Developer
Developer
 
Posts: 35
Joined: Tue Nov 18, 2008 3:43 pm

Postby alsher » Thu Sep 24, 2009 7:41 pm

Is it possible to post the key strokes form background app?
alsher
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Sep 24, 2009 7:36 pm

Re: Throwing/Simulating KeyStrokes programatically

Postby vanny » Thu Jan 14, 2010 4:28 am

how to do it in android sdk 1.5 ?

in android sdk 1.5 ,i get some error:

import android.os.ServiceManager; //The import android.os.ServiceManager cannot be resolved
import android.view.IWindowManager; //The import android.view.IWindowManager cannot be resolved
vanny
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jan 14, 2010 4:22 am

Postby bandarbilla » Sun Apr 25, 2010 7:43 am

plusminus... I have got so much help from u through this forum in last one year !!! I love u man !!!
bandarbilla
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 25, 2010 7:41 am

Postby mandroid » Tue Apr 27, 2010 9:18 am

Hi,

Im trying to realize something similar, posted on this thread. My intention was to build a Service, which creates KeyEvents like (DPAD_UP, DPAD_DOWN ...). I have already tried every method, and none of them is working! The Method in this thread is not working or no more available in my SDK (rev 5).

I found this blog on developer.android.com : http://developer.android.com/resources/ ... ethod.html .

There described, how to create an inputmethodservice. And there are two ways described of sending KeyEvents:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputConnection ic = getCurrentInputConnection();
  2.  
  3. long eventTime = SystemClock.uptimeMillis();
  4.  
  5. ic.sendKeyEvent(new KeyEvent(eventTime, eventTime,
  6.  
  7.     KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0,
  8.  
  9.     KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
  10.  
  11. ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
  12.  
  13.     KeyEvent.ACTION_UP, keyEventCode, 0, 0, 0, 0,
  14.  
  15.     KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  InputMethodService.sendDownUpKeyEvents(keyEventCode);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


This ways to send a KeyEvent did not work in my app. I also tried it by creating a InputMethodService and send the KeyEvent there.

The Tutorial in this thread uses the method windowManager.injectKeyEvent. This one is not available in my SDK?
My target device is a device with Android 2.1. Later i will do the same for an Android 1.6 device.

This is my Code where i try to catch a HW-KeyEvent and "remap" it by sending a different keyCode. This is just to find any function/method that can send a KeyEvent.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {
  2.  
  3.                
  4.  
  5.                 int      keyC = keyCode;
  6.  
  7.                
  8.  
  9.                 InputMethodService keyIMS = new InputMethodService();
  10.  
  11.                
  12.  
  13.                 if (keyC == KeyEvent.KEYCODE_DPAD_UP){
  14.  
  15.                        
  16.  
  17.                         keyIMS.sendDownUpKeyEvents(KeyEvent.KEYCODE_VOLUME_UP);
  18.  
  19.                        
  20.  
  21.                 }
  22.  
  23.                
  24.  
  25.                 if (keyC == KeyEvent.KEYCODE_DPAD_DOWN){
  26.  
  27.                         keyIMS.sendDownUpKeyEvents(KeyEvent.KEYCODE_VOLUME_DOWN);
  28.  
  29.                 }
  30.  
  31.                
  32.  
  33.                 return true;
  34.  
  35.                
  36.  
  37.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


i woul appreciate if anyone could give me a good hint.

greetings..
mandroid
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 27, 2010 8:47 am

Top

Postby pskink » Tue Apr 27, 2010 9:34 am

mandroid wrote:Hi,

Im trying to realize something similar, posted on this thread. My intention was to build a Service, which creates KeyEvents like (DPAD_UP, DPAD_DOWN ...). I have already tried every method, and none of them is working! The Method in this thread is not working or no more available in my SDK (rev 5).


its very bad idea to send/inject key events

why do you need it?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby mandroid » Tue Apr 27, 2010 10:19 am

Hi pskink,

i want to write an app, which can remote control an Android-Device. The remote-aspect is another problem. First of all i want to find out how to inject KeyEvents. Afterwards i guess im' going to try sending keyCodes over USB/Bluetooth or socket to an Android-Device.

Why is it a bad idea to inject KeyEvents? Is it generally a bad idea, or is it bad in the way i am doing it :) ?
mandroid
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 27, 2010 8:47 am

Postby pskink » Tue Apr 27, 2010 10:37 am

mandroid wrote:Hi pskink,

i want to write an app, which can remote control an Android-Device. The remote-aspect is another problem. First of all i want to find out how to inject KeyEvents. Afterwards i guess im' going to try sending keyCodes over USB/Bluetooth or socket to an Android-Device.

Why is it a bad idea to inject KeyEvents? Is it generally a bad idea, or is it bad in the way i am doing it :) ?


if you to call some action which is invoked when you press some key, why not to call it directly ?

key events are propagated to focused view, do you have any control which view is focused or not?

and finally even if you could send key event you could send it only to your app
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby mandroid » Tue Apr 27, 2010 10:45 am

if you to call some action which is invoked when you press some key, why not to call it directly ?

key events are propagated to focused view, do you have any control which view is focused or not?

and finally even if you could send key event you could send it only to your app


this is why i try to realize it within a service. No i have no control about focused views. The idea, that i need to get a focused view came to me 5 min ago :( . Now i'm searching inside the Android reference for a method to get the current focused view. Maybe you know a method to do this?
mandroid
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 27, 2010 8:47 am

Postby pskink » Tue Apr 27, 2010 11:45 am

mandroid wrote:this is why i try to realize it within a service.

why?

mandroid wrote:No i have no control about focused views. The idea, that i need to get a focused view came to me 5 min ago :( . Now i'm searching inside the Android reference for a method to get the current focused view. Maybe you know a method to do this?


this is why i call it bad idea. why not to call your action directly instead of sending some key events?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby mandroid » Tue Apr 27, 2010 12:05 pm

> why?

my thought was, if i perform this whithin a service is globally available. In fact i want to realize an app that is comparable to a VNC-Server on a PC, but without an Screen. I want to have control of the Androids Keys from remote. For this purpose, so i think, in need to have a method to inject KeyEvents.

Or, to describe my intend in a simplified way:

I want to press a "UP" on my Computer and get the appropriate reaction (DPAD_UP) on my Android-device (and so on ...)

you could call this a "remote keyboard" for android :)

Furthermore is still don't get it, why it should be a bad idea to inject KeyEvents (sorry :?)
mandroid
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 27, 2010 8:47 am

Postby pskink » Tue Apr 27, 2010 12:26 pm

mandroid wrote:Or, to describe my intend in a simplified way:

I want to press a "UP" on my Computer and get the appropriate reaction (DPAD_UP) on my Android-device (and so on ...)

you could call this a "remote keyboard" for android :)

Furthermore is still don't get it, why it should be a bad idea to inject KeyEvents (sorry :?)


so your app has no gui at all and you want to control other apps by sending key events to them?

from obvious security reasons its impossible

imagine you have file browser which can delete any file on your phone.

would you like some service to access it and delete some of your data without your knowledge?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby mandroid » Tue Apr 27, 2010 1:02 pm

so your app has no gui at all and you want to control other apps by sending key events to them?


A gui is not that important. I have implemented one, to start and stop the service. Yes generally controling the device and It's apps - this is what i want to do from a remote computer, over Bluetooth or USB-Connection.

from obvious security reasons its impossible


nope, via "android debug bridge" something like this is possible. If you connect to the device-shell over "adb shell" you can inject KeyEvents with the shell-command "input keyevent <keycode>". Don't know whether the device needs to be rooted, mine is rooted :).
This what the "input" command do, is what i basically want to do. Now i thougt maybe there is a more elegant way to do this via writing an app, that receives keycodes and injects keyevents. I dont want to distribute my app anyway. This "remote-keyoard" is mentioned for test-purposes in a lab-environment...

would you like some service to access it and delete some of your data without your knowledge?


I dont need a remote-keyboard to delete some files on a device, or do i :) ? Yes maybe this would be possible, but its not in my scope. The same refers to a VNC-Server. Of course, it is possible to delete files, install "bad things" on a remote computer, with a VNC. But "usually" this is not the users interest.

Hmm, i guess this leads to an off-topic. Maybe i create a new Thread. Don't want to overload this one ..
mandroid
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 27, 2010 8:47 am

Postby pskink » Tue Apr 27, 2010 1:32 pm

mandroid wrote:
from obvious security reasons its impossible


nope, via "android debug bridge" something like this is possible. If you connect to the device-shell over "adb shell" you can inject KeyEvents with the shell-command "input keyevent <keycode>".


see: http://groups.google.com/group/android- ... cd8adf3a41

Diane is google engineer working on android so i would believe her...
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests