Capturing keyevent

Put your problem here if it does not fit any of the other categories.

Capturing keyevent

Postby falcon » Thu Feb 21, 2008 6:55 am

Hey plusminus,
great work!!!!
I wanted to know that how can we capture a keypress and to display it on the screen......for example if I press J on the android mobile keypad then the screen should display J on the screen.[font=Arial] [/font]
falcon
Freshman
Freshman
 
Posts: 2
Joined: Thu Feb 21, 2008 6:44 am

Top

Postby venkat » Thu Feb 21, 2008 9:48 am

Hi falcon, This is very simple.

mail.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView  android:id="@+id/key"
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="Hello World, keypress"
  11.    />
  12. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


That's it. :)


keypress.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.KeyEvent;
  4. import android.widget.TextView;
  5.  
  6. public class keypress extends Activity {
  7.     /** Called when the activity is first created. */
  8.         TextView tv;
  9.         @Override
  10.     public void onCreate(Bundle icicle) {
  11.         super.onCreate(icicle);
  12.         setContentView(R.layout.main);
  13.         tv=(TextView)findViewById(R.id.key);
  14.                 tv.setText("Press Any Key");
  15.                
  16.                
  17.     }
  18.  
  19.  
  20.        
  21.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  22.                 // TODO Auto-generated method stub
  23.            
  24.                 //get the key code and convert into ASCII Code.
  25.                 int keypressed='A'+keyCode-29;
  26.                
  27.                 //Convert ASCII Code into String.  and show it.
  28.                 tv.setText("You have pressed:  "+(char)keypressed);
  29. return super.onKeyDown(keyCode, event);
  30.         }
  31. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


It will work only for "A - Z " Thats it :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby falcon » Thu Feb 21, 2008 12:18 pm

Thanks a lot[font=Arial] [/font]
falcon
Freshman
Freshman
 
Posts: 2
Joined: Thu Feb 21, 2008 6:44 am

Postby venkat » Thu Feb 21, 2008 12:23 pm

you are welcome. Feel free to ask you doubt. :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 21 guests