首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

记录|如何全局监听鼠标和键盘等事件

  • 25-02-21 21:01
  • 3590
  • 12907
blog.csdn.net

目录

  • 前言
  • 一、MyMessager类
  • 二、Form中进行Timer监听
  • 更新时间


前言

参考文章:
C# winfrom 长时间检查不到操作,自动关闭应用程序

本来是想,如果一段时间没有操作软件,这个软件就自动退出的任务。但是在C#中,采用winform后,有很大的困难。表现为:

  • 监听困难,理论上是只要进行Form监听就可以了,但是实际上Form上面有许多的Panel和Button等控件在上面进行监听,所以如果要写鼠标、按钮等监听事件,会导致要N多个控件的重复性写法。
  • 一查找这种全局监听的,很多CSDN中都采用HOOK钩子的写法。而这种写法一则我没用过,二则会和系统抢资源,属于影响性能的一种方法。

后来,看了上面的文章后,自己进行了实验,发现这种方法确实可行。现在将复刻后的代码公布如下:


一、MyMessager类

这里创建个MyMessager类,对Messager信息进行监听。【类的代码如下:】

  • iOperCount,创建为public类,是为了调用的时候来进行运行时间判断。
namespace ZHCH_winform_2.manager
{
    internal class MyMessager : IMessageFilter
    {
        public int iOperCount { get; set; }

        public bool PreFilterMessage(ref Message m)
        {
            // 如果检测到有鼠标或则键盘的消息,则使计数为0
            if(m.Msg==0x0200 || m.Msg==0x0201 || m.Msg==0x0204 || m.Msg == 0x0207)
            {
                iOperCount = 0;
            }
            return false;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

二、Form中进行Timer监听

  • 初始化MyMessager类msg。
        private MyMessager msg = new MyMessager();
        //
        public FormMain()
        {
            InitializeComponent();
            this.KeyPreview = true; // 允许窗体接收按键事件
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 在Form类FormMain加载时,进行消息监听【采用了定时器Timer,命名为:timerMoniter】

        private void FormMain_Load(object sender, EventArgs e)
        {
            Application.AddMessageFilter(msg);
            timerMoniter.Start();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 定时器中的监听事件如下:
        /// 
        /// 全局监控鼠标、键盘等事件
        /// 
        /// 
        /// 
        private void timerMoniter_Tick(object sender, EventArgs e)
        {
            //如果计数=0,代表触发了按钮,鼠标等事件,那么就要重新计时。
            if (msg.iOperCount == 0)
            {
                ResetLogoutSeconds();
            }
            int iOperCount = msg.iOperCount++;
            // 如果计数超过了设定的最大时间,就退出登录
            if (iOperCount > this.MaxSeconds || logoutSeconds <= 0)
            {
                MessageBox.Show($"{this.MaxSeconds}已经到达");
                Application.Exit();
            }
            // 倒计时的循环
            if (logoutSeconds > 0)
            {
                this.progressLogout.Text = logoutSeconds.ToString() + " S";
                float progressValue = (float)(logoutSeconds / MaxSeconds);
                this.progressLogout.Value = progressValue;
                logoutSeconds--;
            }
        }
        /// 
        /// 重写键盘读取事件
        /// 
        /// 
        protected override void OnKeyDown(KeyEventArgs e)
        {
            ResetLogoutSeconds();
            //base.OnKeyDown(e);
        }
        /// 
        /// 重置倒计时时间
        /// 
        private void ResetLogoutSeconds()
        {
            logoutSeconds = 120;
            progressLogout.Text = logoutSeconds.ToString() + " S";
            this.progressLogout.Value = 1;
        }
  • 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

更新时间

  • 2024.08.26:创建并复刻成功
注:本文转载自blog.csdn.net的小白鼠零号的文章"https://blog.csdn.net/qq_41714549/article/details/141572966"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

138
3C硬件
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top