title: How to get click event of keyboard in android?
body: <p>Is their any way to get click event of keyboard in android? Actually I am developing one app and need the click event of the keyboard. Example, if user press the alphabets 'a' from keyboard in android phone then I want the click event of 'a'. Recently I came across the onKeyListener and used the various KeyCode, but eventually it didn't helped me. </p>
  
  <pre><code>public class KeyCode {
  
  
      private Context con;
      private EditText edt;
      private ArrayList&lt;String&gt; array;
  
  
      public KeyCode(Context con, final ArrayList&lt;String&gt; array, final EditText edt)
      {
  
          // TODO Auto-generated constructor stub
  
          this.array=array;
          this.con=con;
          this.edt=edt;
  
  
          edt.setOnKeyListener(new View.OnKeyListener() {
              @Override
              public boolean onKey(View v, int keyCode, KeyEvent event) {
  
  
                  if(keyCode==event.KEYCODE_A)
                  {
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(0));
                      }
                      else
                      {
                          edt.append(array.get(26));
                      }
                  }
                  if(keyCode==event.KEYCODE_B)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(1));
                      }
                      else
                      {
                          edt.append(array.get(27));
                      }
  
                  }
                  if(keyCode==event.KEYCODE_C)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(2));
                      }
                      else
                      {
                          edt.append(array.get(28));
                      }
                  }
                  if(keyCode==event.KEYCODE_D)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(3));
                      }
                      else
                      {
                          edt.append(array.get(29));
                      }
                  }
                  if(keyCode==event.KEYCODE_E)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(4));
                      }
                      else
                      {
                          edt.append(array.get(30));
                      }
                  }
                  if(keyCode==event.KEYCODE_F)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(5));
                      }
                      else
                      {
                          edt.append(array.get(31));
                      }
                  }
                  if(keyCode==event.KEYCODE_G)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(6));
                      }
                      else
                      {
                          edt.append(array.get(32));
                      }
                  }
                  if(keyCode==event.KEYCODE_H)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(7));
                      }
                      else
                      {
                          edt.append(array.get(33));
                      }
                  }
                  if(keyCode==event.KEYCODE_I)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(8));
                      }
                      else
                      {
                          edt.append(array.get(34));
                      }
                  }
                  if(keyCode==event.KEYCODE_J)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(9));
                      }
                      else
                      {
                          edt.append(array.get(35));
                      }
                  }
                  if(keyCode==event.KEYCODE_K)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(10));
                      }
                      else
                      {
                          edt.append(array.get(36));
                      }
                  }
                  if(keyCode==event.KEYCODE_L)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(11));
                      }
                      else
                      {
                          edt.append(array.get(37));
                      }
                  }
                  if(keyCode==event.KEYCODE_M)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(12));
                      }
                      else
                      {
                          edt.append(array.get(38));
                      }
                  }
                  if(keyCode==event.KEYCODE_N)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(13));
                      }
                      else
                      {
                          edt.append(array.get(39));
                      }
                  }
                  if(keyCode==event.KEYCODE_O)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(14));
                      }
                      else
                      {
                          edt.append(array.get(40));
                      }
                  }
                  if(keyCode==event.KEYCODE_P)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(15));
                      }
                      else
                      {
                          edt.append(array.get(41));
                      }
                  }
                  if(keyCode==event.KEYCODE_Q)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(16));
                      }
                      else
                      {
                          edt.append(array.get(42));
                      }
                  }
                  if(keyCode==event.KEYCODE_R)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(17));
                      }
                      else
                      {
                          edt.append(array.get(43));
                      }
                  }
                  if(keyCode==event.KEYCODE_S)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(18));
                      }
                      else
                      {
                          edt.append(array.get(44));
                      }
                  }
                  if(keyCode==event.KEYCODE_T)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(19));
                      }
                      else
                      {
                          edt.append(array.get(45));
                      }
                  }
                  if(keyCode==event.KEYCODE_U)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(20));
                      }
                      else
                      {
                          edt.append(array.get(46));
                      }
                  }
                  if(keyCode==event.KEYCODE_V)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(21));
                      }
                      else
                      {
                          edt.append(array.get(47));
                      }
                  }
                  if(keyCode==event.KEYCODE_W)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(22));
                      }
                      else
                      {
                          edt.append(array.get(48));
                      }
                  }
                  if(keyCode==event.KEYCODE_X)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(23));
                      }
                      else
                      {
                          edt.append(array.get(49));
                      }
                  }
                  if(keyCode==event.KEYCODE_Y)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(24));
                      }
                      else
                      {
                          edt.append(array.get(50));
                      }
                  }
                  if(keyCode==event.KEYCODE_Z)
                  {
  
                      if(event.isCapsLockOn())
                      {
                          edt.append(array.get(25));
                      }
                      else
                      {
                          edt.append(array.get(51));
                      }
                  }
  
  
                  if(keyCode==event.KEYCODE_0)
                  {
                      edt.append(array.get(52));
                  }
                  if(keyCode==event.KEYCODE_1)
                  {
                      edt.append(array.get(53));
                  }
                  if(keyCode==event.KEYCODE_2)
                  {
                      edt.append(array.get(54));
                  }
                  if(keyCode==event.KEYCODE_3)
                  {
                      edt.append(array.get(55));
                  }
                  if(keyCode==event.KEYCODE_4)
                  {
                      edt.append(array.get(56));
                  }
                  if(keyCode==event.KEYCODE_5)
                  {
                      edt.append(array.get(57));
                  }
                  if(keyCode==event.KEYCODE_6)
                  {
                      edt.append(array.get(58));
                  }
                  if(keyCode==event.KEYCODE_7)
                  {
                      edt.append(array.get(59));
                  }
                  if(keyCode==event.KEYCODE_8)
                  {
                      edt.append(array.get(60));
                  }
                  if(keyCode==event.KEYCODE_9)
                  {
                      edt.append(array.get(61));
                  }
  
                  /*if(keyCode==event.KEYCODE_DEL)
                  {
                      edt.addTextChangedListener(new TextWatcher() {
                          @Override
                          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
                          }
  
                          @Override
                          public void onTextChanged(CharSequence s, int start, int before, int count) {
  
                          }
  
                          @Override
                          public void afterTextChanged(Editable s) {
  
                              edt.setText(s);
  
                          }
                      });
                  }*/
  
                  /*if(keyCode==event.KEYCODE_SPACE)
                  {
                      edt.append(" ");
                  }*/
  
                  if(keyCode==event.KEYCODE_COMMA)
                  {
                      edt.append(",");
                  }
  
                  if(keyCode==event.KEYCODE_PERIOD)
                  {
                      edt.append(".");
                  }
  
                  if(keyCode==event.KEYCODE_EQUALS)
                  {
                      edt.append("=");
                  }
                  if(keyCode==event.KEYCODE_LEFT_BRACKET)
                  {
                      edt.append("(");
                  }
                  if(keyCode==event.KEYCODE_RIGHT_BRACKET)
                  {
                      edt.append(")");
                  }
                  if(keyCode==event.KEYCODE_SEMICOLON)
                  {
                      edt.append(";");
                  }
                  if(keyCode==event.KEYCODE_APOSTROPHE)
                  {
                      edt.append("'");
                  }
                  if(keyCode==event.KEYCODE_GRAVE)
                  {
                      edt.append("~");
                  }
                  if(keyCode==event.KEYCODE_SLASH)
                  {
                      edt.append("/");
                  }
                  if(keyCode==event.KEYCODE_NUMPAD_DIVIDE)
                  {
                      edt.append("/");
                  }
                  if(keyCode==event.KEYCODE_NUMPAD_MULTIPLY)
                  {
                      edt.append("*");
                  }
                  if(keyCode==event.KEYCODE_NUMPAD_SUBTRACT)
                  {
                      edt.append("-");
                  }
                  if(keyCode==event.KEYCODE_NUMPAD_ADD)
                  {
                      edt.append("+");
                  }
  
  
  
                  return true;
              }
          });
  
      }
  
  
  
  }
  </code></pre>
  
  <p>This is what my java class is. I have an array of certain characters and I do pass it to this java file to replace the default characters with characters contained in array. The characters are replaced but when the key is pressed from the laptop keyboard not from the keyboard of the android phone. </p>
  
  <pre><code>public class ChangeFont extends ActionBarActivity {
  
      private EditText edt;
      private ImageView img1,img2,img3,img4,img5;
      private ListView lst, savelist;
      private TextView txtNew,txtSave;
      private DataBaseHelper data;
      private ArrayList&lt;String&gt; category;
      private ArrayList&lt;String&gt; styleArray;
      private Cursor c;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_change_font);
  
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setHomeButtonEnabled(true);
          getSupportActionBar().setTitle("Fonts");
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
  
  
          data=new DataBaseHelper(this);
          edt=(EditText)findViewById(R.id.edit_text);
  
          img1=(ImageView)findViewById(R.id.image1);
          img2=(ImageView)findViewById(R.id.image2);
          img3=(ImageView)findViewById(R.id.image3);
          img4=(ImageView)findViewById(R.id.image4);
          img5=(ImageView)findViewById(R.id.image5);
  
          lst=(ListView)findViewById(R.id.list);
          savelist=(ListView)findViewById(R.id.savelist);
  
          txtNew=(TextView)findViewById(R.id.textnew);
          txtSave=(TextView)findViewById(R.id.textsave);
  
          category=new ArrayList&lt;String&gt;();
          styleArray=new ArrayList&lt;String&gt;();
  
          try {
              data.createDataBase();
  
  
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          c=data.getSample();
  
  
          if (c.moveToFirst())
          {
              while(!c.isAfterLast())
              {
                  String cat = c.getString(c.getColumnIndex(DataBaseUtil.SAMPLE));
  
  
                  category.add(cat);
  
                  c.moveToNext();
  
              }
          }
  
  
          /*l=data.getAllLockType();
  
          if (l.moveToFirst())
          {
              while(!l.isAfterLast())
              {
  
                  String cat=l.getString(l.getColumnIndex(DataBaseUtil.LOCK_TYPE));
  
                  Log.i("String",""+cat);
                  locktype.add(cat);
  
                  l.moveToNext();
  
              }
          }
  
          Log.i("Lock Array",""+locktype);*/
  
          SampleStyleAdapter aa=new SampleStyleAdapter(getApplicationContext(), category);
  
          lst.setAdapter(aa);
  
  
  
          lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                  data.openDataBase();
  
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
  
  
                  final String stylename = category.get(position).toString();
  
                  Toast.makeText(getApplicationContext(), "" + stylename + "position" + position, Toast.LENGTH_SHORT).show();
  
                  List&lt;DataBaseModel&gt; sy = data.getStyle(position);
  
  
                  styleArray = new ArrayList&lt;String&gt;();
                  for (DataBaseModel model : sy) {
                      String vimal = model.getStyle();
  
                      styleArray.add(vimal);
                  }
  
                  Log.i("Style Array", "" + styleArray);
  
  
                  KeyCode key=new KeyCode(getApplicationContext(),styleArray,edt);
                  //Log.i("Called Key Code","Class");
  
  
  
              }
          });
  
          edt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  lst.setVisibility(View.GONE);
                  savelist.setVisibility(View.GONE);
                  txtNew.setVisibility(View.VISIBLE);
                  txtSave.setVisibility(View.VISIBLE);
                  edt.requestFocus();
                  edt.setCursorVisible(true);
                  //InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  //imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
  
              }
          });
  
          img1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  lst.setVisibility(View.VISIBLE);
                  savelist.setVisibility(View.GONE);
                  txtNew.setVisibility(View.GONE);
                  txtSave.setVisibility(View.GONE);
                  edt.setCursorVisible(false);
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
  
              }
          });
  
          img2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  lst.setVisibility(View.GONE);
                  txtNew.setVisibility(View.VISIBLE);
                  txtSave.setVisibility(View.VISIBLE);
                  edt.requestFocus();
                  edt.setCursorVisible(true);
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT);
  
  
              }
          });
  
          img3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Intent i=new Intent(ChangeFont.this, EmojiTabbed.class);
                  startActivity(i);
  
              }
          });
  
          img4.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  lst.setVisibility(View.GONE);
                  savelist.setVisibility(View.VISIBLE);
                  txtNew.setVisibility(View.GONE);
                  txtSave.setVisibility(View.GONE);
                  edt.setCursorVisible(false);
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
  
              }
          });
  
          img5.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Intent i=new Intent(ChangeFont.this, Share.class);
                  startActivity(i);
  
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_change_font, menu);
          return false;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          switch (item.getItemId()) {
              // Respond to the action bar's Up/Home button
              case android.R.id.home:
  
                  Intent i=new Intent(this, Home.class);
                  NavUtils.navigateUpTo(this, i);
                  return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  
  } 
  </code></pre>
  
  <p>Whether this thing is possible or not? </p>