Code example for KeyEvent

Methods: getActiongetKeyCode

0
	} 
	 
	@Override 
	public boolean onKeyPreIme(int keyCode, KeyEvent event)
	{ 
		return pref.onKey(null, event.getKeyCode(), event);
	} 
	 
	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{ 
		return pref.onKey(null, event.getKeyCode(), event);
	} 
} 
 
class KeyBindPreference extends DialogPreference implements View.OnKeyListener, AdapterView.OnItemClickListener, LayoutInflater.Factory {
	private int key_bind_code;
	private boolean grabKeyCode = false;
	KeyBindEditText keyText;
	private String[] key_labels;
	private final int DEFAULT_KEYCODE = 0;
 
	public KeyBindPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		key_labels = getContext().getResources().getStringArray(R.array.key_bind_values);
	} 
	 
	private void setKey(int keyCode, boolean force)
	{ 
		if (grabKeyCode || force)
		{ 
			grabKeyCode = false;
			key_bind_code = keyCode;
			keyText.setText("Current: " + key_labels[key_bind_code]);
		} 
	} 
 
	@Override 
	protected void onBindDialogView(View view) {
	    super.onBindDialogView(view);
	} 
	 
	@Override 
	protected View onCreateDialogView()
	{ 
		LayoutInflater inflater = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).cloneInContext(getContext());
		inflater.setFactory(this);
		View view = inflater.inflate(R.layout.key_bind_dialog, null);
		keyText = (KeyBindEditText) view.findViewById(R.id.key_bind_value);
		keyText.setBoundPreference(this);
		view.setOnKeyListener(this);
		((ListView) view.findViewById(R.id.key_bind_list)).setOnItemClickListener(this);
		((Button) view.findViewById(R.id.key_bind_clear)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        		setKey(0, true); 
            } 
        }); 
		((Button) view.findViewById(R.id.key_bind_detect)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	grabKeyCode = true;
    			keyText.setText("Press key to use");
    			keyText.requestFocus();
            } 
        }); 
		InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(keyText.getWindowToken(), 0);
		setKey(getPersistedInt(DEFAULT_KEYCODE), true);
		return view;
	} 
 
	@Override 
	protected void onDialogClosed(boolean positiveResult) {
	   super.onDialogClosed(positiveResult);
	 
	    if (positiveResult) {
	    	persistInt(key_bind_code);
	    	notifyChanged(); 
	    } 
	} 
	 
	@Override 
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (grabKeyCode && event.getAction() == KeyEvent.ACTION_DOWN && keyCode != 0 && keyCode < key_labels.length)
		{ 
			setKey(keyCode, false);
			return true; 
		} 
		return false;