基于TCP和多线程实现无线鼠标键盘-GestureDetector
为了实现无线鼠标,需要识别出用户在手机屏幕上的滑动动作,这就需要用到GestureDetector类。
首先是activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_marginRight= "20dp" android:orientation= "vertical" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" > <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <Button android:id= "@+id/btn_options" style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "fill_parent" android:layout_weight= "1" android:text= "设置" /> <Button android:id= "@+id/btn_connect" style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "fill_parent" android:layout_weight= "1" android:text= "连接" /> </LinearLayout> <TextView android:id= "@+id/txt_mouse" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_marginBottom= "5dp" android:layout_marginTop= "5dp" android:text= "鼠标灵敏度:" /> <SeekBar android:id= "@+id/skb_mouse" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_marginLeft= "20dp" android:max= "400" android:progress= "100" /> <LinearLayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <Button android:id= "@+id/btn_left" style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "fill_parent" android:layout_weight= "1" android:text= "左键" /> <Button android:id= "@+id/btn_right" style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "fill_parent" android:layout_weight= "1" android:text= "右键" /> </LinearLayout> <TextView android:id= "@+id/txt_touch" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_weight= "1" /> <Button android:id= "@+id/btn_keyboard" style= "?android:attr/buttonStyleSmall" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical|center_horizontal" android:text= "键盘" /> </LinearLayout> |
运行后的效果:
中间的空白区即是用户操作鼠标的区域,为了识别用户的动作,定义Mouse_GestureListener类,该类继承自GestureDetector.SimpleOnGestureListener:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Mouse_GestureListener extends GestureDetector.SimpleOnGestureListener{ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { MainActivity.dis_x = e2.getX()-e1.getX(); MainActivity.dis_y = e2.getY()-e1.getY(); // 移动距离是否足够 if (( float )Math.pow((Math.pow(MainActivity.dis_x, 2 )+Math.pow(MainActivity.dis_y, 2 )), 0.5 )<br> >MainActivity.dis_t){ MainActivity.dis_x *= MainActivity.move_times; MainActivity.dis_y *= MainActivity.move_times; MainActivity.send_thread.set_str(MainActivity.df2.format(MainActivity.dis_x)+<br> "/" +MainActivity.df2.format(MainActivity.dis_y)); } return true ; } |
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)即手指在屏幕上滑动时的事件,e1是第一个点,e2是第二个点,计算这两个点的x坐标和y坐标之差,就是这次滑动在x轴和y轴上移动的距离,并且将计算出来的距离乘以鼠标灵敏度,交给发送线程发送给Windows端。
在MainActivity类中定义:
1 | GestureDetector gd; |
在onCreate(Bundle savedInstanceState)方法中加上一句:
1 | gd = new GestureDetector( this , new Mouse_GestureListener()); |
还要在MainActivity类中定义方法
1 2 3 4 5 6 7 8 | public boolean onTouchEvent(MotionEvent event) { if (gd.onTouchEvent(event)) return true ; else return false ; } |
这样,就可以识别出用户在手机屏幕上操作鼠标的动作,并且发送给Windows端。
分类:
Android
标签:
无线鼠标键盘
, GestureDetector
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话