如何在安卓设备上制作 KeyDown 和 KeyUp?

我有个问题。

我正在Android 设备上制作Keystroke 动态应用程序。

现在,我用度量字符串和EditText. 我想在软件键盘上捕捉KeyDownKeyUp事件。

我的问题是,使用 Java 在 Android 上捕获KeyUp和使用的最佳方法是什么?KeyDown如果EditText是一个好的选择?如果它有方法来捕捉任何按键?

http://img3.mukewang.com/6288d8900001730e02540352.jpg

编辑


我想从上面的字符串中检测键并测量按下它的时间,(例如开始测量KeyDown和停止KeyUp)。如果可能的话,我想阻止我的test string(它的9RJhl6aH0n,就像我的屏幕中一样)中未提及的其他键


编辑2


到目前为止我所取得的成就是这样的,但是default当我编码 line: 时,我的应用程序崩溃了measureText.setText("")。它工作得很好,但它仍然不会触发KeyDown(或KeyPress)。这些方法仅在KeyUp用户刚刚输入字母时运行。顺序很重要!


measureText.addTextChangedListener(new TextWatcher(){

        @Override

        public void afterTextChanged(Editable arg0) {

            switch(measureText.getText().toString()){

                case "9":

                    break;

                case "9R":

                    break;

                case "9RJ":

                    break;

                case "9RJh":

                    break;

                case "9RJhl":

                    break;

                case "9RJhl6":

                    break;

                case "9RJhl6a":

                    break;

                case "9RJhl6a0":

                    break;

                case "9RJhl6a0n":

                    break;

                default:

                    measureText.getText().clear();

                    break;


            }

            return;

        }


        @Override

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub

            return;

        }


        @Override

        public void onTextChanged(CharSequence s, int start, int before, int count) {


            return;

        }

    });


墨色风雨
浏览 233回答 3
3回答

忽然笑

我会说,那OnKeyUp&OnKeyDown事件不会削减它,因为软键盘几乎不会发出任何这些。这是一个粗略的原型,它根据预期的字符串过滤字符的输入。有很大的改进空间;虽然自定义实现仍然是比尝试使用框架方法更好的方法,框架方法可能只捕获⌫键......在FilteredEditText任何输入出现在屏幕上之前捕获它 - 为了实现击键模式记录器,预期的字符串需要被分成一个ArrayList,它也将保持各个击键之间的持续时间;一旦记录下来,就可以使用收集到的信息进行比较。/**&nbsp;* Filtered {@link AppCompatEditText}&nbsp;* @author Martin Zeitler&nbsp;*/public class FilteredEditText extends AppCompatEditText {&nbsp; &nbsp; private static final String LOG_TAG = FilteredEditText.class.getSimpleName();&nbsp; &nbsp; private String expectedString = null;&nbsp; &nbsp; public FilteredEditText(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public FilteredEditText(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public FilteredEditText(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; public void setExpectedString(@NonNull String value) {&nbsp; &nbsp; &nbsp; &nbsp; this.expectedString = value;&nbsp; &nbsp; &nbsp; &nbsp; this.setupInputFilter();&nbsp; &nbsp; }&nbsp; &nbsp; public void setupInputFilter() {&nbsp; &nbsp; &nbsp; &nbsp; this.setFilters(new InputFilter[] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new InputFilter() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int destStart, int destEnd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (source.length() > 0 && source.charAt(end-1) == expectedString.charAt(destEnd)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* valid input received */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(LOG_TAG, "input accepted: " + String.valueOf(source.charAt(end-1)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return source;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* invalid input received */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(LOG_TAG, "input rejected: " + String.valueOf(source.charAt(end-1)) + " - expected: " + String.valueOf(expectedString.charAt(destEnd)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; /** hardware event&nbsp; */&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onKeyDown(int keyCode, KeyEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(LOG_TAG, "onKeyDown()");&nbsp; &nbsp; &nbsp; &nbsp; return super.onKeyDown(keyCode, event);&nbsp; &nbsp; }&nbsp; &nbsp; /** hardware event&nbsp; */&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onKeyUp(int keyCode, KeyEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(LOG_TAG, "onKeyUp()");&nbsp; &nbsp; &nbsp; &nbsp; return super.onKeyUp(keyCode, event);&nbsp; &nbsp; }}使用示例:FilteredEditText mTextInput = findViewById(R.id.text_input);mTextInput.setExpectedString("9RJhl6aH0n");日志输出:D/FilteredEditText: input accepted: 9D/FilteredEditText: input rejected: r - expected: RD/FilteredEditText: input rejected: 4 - expected: RD/FilteredEditText: input accepted: R到目前为止,我已经使用软件键盘对其进行了测试……而我目前无法使用 BT 硬件键盘对其进行测试,因为电池是空的。我假设,InputFilter捕获所有输入。几乎没有任何事件是由软件键盘触发的OnKeyUp,OnKeyDown因为当知道击键何时被过滤时,这仍然会导致类似的模式——即使无法测量击键的持续时间,也无法测量键盘的攻击速度。击键,由于软件键盘的限制 - 唯一可能的解决方法是强制使用硬件键盘或创建一个为所有键发出这些事件的软件键盘(与默认值相反GBoard,也不是SwiftKey)。我现在只是想知道滑动打字和语音打字......因为这是物理击键动力学几乎没有考虑过的事情。甚至留下了反馈GBoard,因为在某些情况下选择性地发出键码会很有帮助。该文档还明确指出:在使用类和相关 API 处理键盘事件时KeyEvent,您应该期望此类键盘事件仅来自硬件键盘。您永远不应依赖接收软输入法(屏幕键盘)上任何键的键事件。人们仍然可以使用硬件事件,同时拥有发出它们的按钮;例如:/**&nbsp;* Fake Hardware {@link AppCompatButton}&nbsp;* @see <a href="https://developer.android.com/reference/android/view/KeyEvent">KeyEvent</a>&nbsp;* @author Martin Zeitler&nbsp;*/public class FakeHardwareButton extends AppCompatButton {&nbsp; &nbsp; private BaseInputConnection&nbsp; mInputConnection;&nbsp; &nbsp; private int keyCode = KeyEvent.KEYCODE_9;&nbsp; &nbsp; private KeyEvent keyDown;&nbsp; &nbsp; private KeyEvent keyUp;&nbsp; &nbsp; public FakeHardwareButton(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, null);&nbsp; &nbsp; }&nbsp; &nbsp; public FakeHardwareButton(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; this(context, attrs, 0);&nbsp; &nbsp; }&nbsp; &nbsp; public FakeHardwareButton(Context context, AttributeSet attrs, int defStyleAttr) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyleAttr);&nbsp; &nbsp; }&nbsp; &nbsp; @SuppressLint("ClickableViewAccessibility")&nbsp; &nbsp; private void setupInputConnection(View targetView) {&nbsp; &nbsp; &nbsp; &nbsp;this.mInputConnection = new BaseInputConnection(targetView, true);&nbsp; &nbsp; &nbsp; &nbsp;this.keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, this.keyCode);&nbsp; &nbsp; &nbsp; &nbsp;this.keyUp = new KeyEvent(KeyEvent.ACTION_UP, this.keyCode);&nbsp; &nbsp; &nbsp; &nbsp;this.setOnTouchListener(new View.OnTouchListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouch(View v, MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch(event.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mInputConnection.sendKeyEvent(keyDown);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mInputConnection.sendKeyEvent(keyUp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}问题只是,例如。KeyEvent.KEYCODE_9并且KeyEvent.KEYCODE_NUMPAD_9不一样,因此String在数字键的情况下总是必须比较表示。

凤凰求蛊

您只需使用活动的调度事件像这样:&nbsp;@Override&nbsp; &nbsp; public boolean dispatchTouchEvent(MotionEvent ev) {&nbsp; &nbsp; &nbsp; &nbsp; switch (ev.getAction() & MotionEvent.ACTION_MASK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_POINTER_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_POINTER_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.dispatchTouchEvent(ev);&nbsp; &nbsp; }这样,您可以轻松控制 KeyDown 和 KeyUp 事件。在这里你可以找到所有的 MotionEvents对于软键,您只能使用TextChangeListener&nbsp; &nbsp; editText.addTextChangedListener(new TextWatcher(){&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; switch (arg0.toString()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9R":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJ":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJh":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJhl":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJhl6":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJhl6a":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJhl6a0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "9RJhl6a0n":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arg0.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int after) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here right logic for getting last char and show it on toast&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp;&nbsp;

浮云间

您需要仔细覆盖以下功能:@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {&nbsp; &nbsp; switch (keyCode) {&nbsp; &nbsp; &nbsp; &nbsp; case KeyEvent.KEYCODE_A:&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your Action code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return super.onKeyDown(keyCode, event);}对于edittext:mMyEditText.addTextChangedListener(new TextWatcher(){&nbsp; &nbsp; public void afterTextChanged(Editable s)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count, int after)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java