获取按键键值的工具种

获取按键键值的工具类

获取当前按下的按键键值,直接新建工程,把这段代码放到主Activity里就行了。

 

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        
        LinearLayout ll = new LinearLayout(this);
        ll.setBackgroundColor(Color.BLUE);
        ll.setOrientation(LinearLayout.VERTICAL);
        
        TextView tv = new TextView(this);
        tv.setText("键值测试程序,请按键开始测试");
        tv.setTextColor(Color.RED);
        ll.addView(tv);
        
        tv2 = new TextView(this);
        tv2.setTextColor(Color.RED);
        ll.addView(tv2);
        
        this.setContentView(ll);
        
    }
    
    TextView tv2;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        tv2.setText("您按下的按键的键值是:"+keyCode);
        
        return super.onKeyDown(keyCode, event);
    }