Android input keyevent
16lz
2021-01-23
http://developer.android.com/reference/android/view/KeyEvent.html
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.commands.input;import android.hardware.input.InputManager;import android.os.SystemClock;import android.util.Log;import android.view.InputDevice;import android.view.KeyCharacterMap;import android.view.KeyEvent;import android.view.MotionEvent;import java.util.HashMap;import java.util.Map;/** * Command that sends key events to the device, either by their keycode, or by * desired character output. */public class Input { private static final String TAG = "Input"; private static final String INVALID_ARGUMENTS = "Error: Invalid arguments for command: "; private static final Map SOURCES = new HashMap() {{ put("keyboard", InputDevice.SOURCE_KEYBOARD); put("dpad", InputDevice.SOURCE_DPAD); put("gamepad", InputDevice.SOURCE_GAMEPAD); put("touchscreen", InputDevice.SOURCE_TOUCHSCREEN); put("mouse", InputDevice.SOURCE_MOUSE); put("stylus", InputDevice.SOURCE_STYLUS); put("trackball", InputDevice.SOURCE_TRACKBALL); put("touchpad", InputDevice.SOURCE_TOUCHPAD); put("touchnavigation", InputDevice.SOURCE_TOUCH_NAVIGATION); put("joystick", InputDevice.SOURCE_JOYSTICK); }}; /** * Command-line entry point. * * @param args The command-line arguments */ public static void main(String[] args) { (new Input()).run(args); } private void run(String[] args) { if (args.length < 1) { showUsage(); return; } int index = 0; String command = args[index]; int inputSource = InputDevice.SOURCE_UNKNOWN; if (SOURCES.containsKey(command)) { inputSource = SOURCES.get(command); index++; command = args[index]; } final int length = args.length - index; try { if (command.equals("text")) { if (length == 2) { inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD); sendText(inputSource, args[index+1]); return; } } else if (command.equals("keyevent")) { if (length >= 2) { final boolean longpress = "--longpress".equals(args[index + 1]); final int start = longpress ? index + 2 : index + 1; inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD); if (length > start) { for (int i = start; i < length; i++) { int keyCode = KeyEvent.keyCodeFromString(args[i]); if (keyCode == KeyEvent.KEYCODE_UNKNOWN) { keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[i]); } sendKeyEvent(inputSource, keyCode, longpress); } return; } } } else if (command.equals("tap")) { if (length == 3) { inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); sendTap(inputSource, Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2])); return; } } else if (command.equals("swipe")) { int duration = -1; inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN); switch (length) { case 6: duration = Integer.parseInt(args[index+5]); case 5: sendSwipe(inputSource, Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2]), Float.parseFloat(args[index+3]), Float.parseFloat(args[index+4]), duration); return; } } else if (command.equals("press")) { inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL); if (length == 1) { sendTap(inputSource, 0.0f, 0.0f); return; } } else if (command.equals("roll")) { inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL); if (length == 3) { sendMove(inputSource, Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2])); return; } } else { System.err.println("Error: Unknown command: " + command); showUsage(); return; } } catch (NumberFormatException ex) { } System.err.println(INVALID_ARGUMENTS + command); showUsage(); } /** * Convert the characters of string text into key event's and send to * device. * * @param text is a string of characters you want to input to the device. */ private void sendText(int source, String text) { StringBuffer buff = new StringBuffer(text); boolean escapeFlag = false; for (int i=0; i] [...]"); System.err.println(); System.err.println("The sources are: "); for (String src : SOURCES.keySet()) { System.err.println(" " + src); } System.err.println(); System.err.println("The commands and default sources are:"); System.err.println(" text (Default: touchscreen)"); System.err.println(" keyevent [--longpress] ..." + " (Default: keyboard)"); System.err.println(" tap (Default: touchscreen)"); System.err.println(" swipe [duration(ms)]" + " (Default: touchscreen)"); System.err.println(" press (Default: trackball)"); System.err.println(" roll (Default: trackball)"); }}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer