How can I grab all keys? (including ENTER key)

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How can I grab all keys? (including ENTER key)

Postby marbalon » Tue Aug 25, 2009 11:59 am

Hi,
I need to catch all keys in my application. The best if I can do this without EditText.

I tried do this with standard View and define onKeyDown() and onKeyUp() and work fine but didn't catch ENTER key. If I put EditText and use setOnKeyListener(), onKey function catch ENTER key but didn't catch other keys :(.

Any ideas how can I grab every keys? Maybe I miss some properties for View or EditText ?

Cheers,
Marcin.
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Top

Postby marbalon » Thu Aug 27, 2009 9:40 am

Here is full source code. I see logs about onKeyDown/Up for all keys except ENTER, and onKey() event not happened.

Code: Select all
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;

public class keylogger extends Activity {

   View v;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        v = (View)findViewById(R.id.View01);
        v.setOnKeyListener( new OnKeyListener() {
         public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            Log.d("test", "onKey");
            return false;
         }
      });
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       // TODO Auto-generated method stub
       Log.d("test", "onKeyDown");
       return super.onKeyDown(keyCode, event);
    }
   
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
       // TODO Auto-generated method stub
       Log.d("test", "onKeyUp");
       return super.onKeyUp(keyCode, event);
    }
}


--------------main.xml--------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<View android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</View>
</LinearLayout>


Cheers,
Marcin.
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Postby arams » Thu Aug 27, 2009 10:00 am

check kecode values

23 & 66

KeyEvent.KEYCODE_DPAD_CENTER
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby marbalon » Thu Aug 27, 2009 10:03 am

arams wrote:check kecode values

23 & 66

KeyEvent.KEYCODE_DPAD_CENTER


Thanks for quick reply but I can get event when press ENTER key, so can check keycode value. But check this on soft keyboard on hw keyboard works :(
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Postby arams » Thu Aug 27, 2009 10:13 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.      public boolean onKeyDown(int keyCode, KeyEvent event) {
  5.  
  6.        
  7.  
  8.                  super.onKeyDown(keyCode, event);        
  9.  
  10.           switch (keyCode) {
  11.  
  12.           case KeyEvent.KEYCODE_DPAD_CENTER:    
  13.  
  14.                   Toast.makeText(this,"KeyDown"+keycode,Toast.LENGTH_SHORT).show();
  15.  
  16.                  return true;
  17.  
  18.      
  19.  
  20.           case 66:    
  21.  
  22.                   Toast.makeText(this,"KeyDown"+keyCode,Toast.LENGTH_SHORT).show();
  23.  
  24.                 return true;
  25.  
  26.        
  27.  
  28.           }
  29.  
  30.           return false;
  31.  
  32.      }
  33.  
  34.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby marbalon » Thu Aug 27, 2009 10:20 am

Yes this works on emulator and on screen keyboard. But when you test it on device and and default soft keyboard nothing happened when you press Enter key. No event = no keycode :(
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Top

Postby arams » Thu Aug 27, 2009 10:26 am

I didnt check in real device.

Are you working in ANdroid 1.5 virtual keypad
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby marbalon » Thu Aug 27, 2009 10:36 am

Yes, but you can open virtual keyboard in emulator too.

I dont know how do this by keys (menu cay not open this keyboard like on real device) but by software you can do little trick...

Code: Select all
...
        case KeyEvent.KEYCODE_DPAD_CENTER:   
          Toast.makeText(this,"KeyDown"+keyCode,Toast.LENGTH_SHORT).show();
          //open sip keyboard
          InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.showSoftInput(v.getRootView(), 0);


and then test enter key on this keyboard.
Thanks for help.
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Postby arams » Thu Aug 27, 2009 10:38 am

Then try

Intercepting hard key events:
http://android-developers.blogspot.com/ ... roid%201.5
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby arams » Thu Aug 27, 2009 10:45 am

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v.getRootView(), 0);


v means(v.getRootView)
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby marbalon » Thu Aug 27, 2009 11:06 am

arams wrote:InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v.getRootView(), 0);


v means(v.getRootView)

This code is not related with my problem, it works and used to open sip keyboard in emulator.

InputMethodService looks promising but have no idea how use it with my code. It is designed to define own keyoard, but how use it to catch soft keyboard keys...
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Postby arams » Thu Aug 27, 2009 12:29 pm

What is the name of the VirtualKeypad view?
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby marbalon » Thu Aug 27, 2009 12:38 pm

arams wrote:What is the name of the VirtualKeypad view?

How can I check name for VK? I'm newbie in android programming...
marbalon
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Aug 25, 2009 11:29 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot], Yahoo [Bot] and 4 guests