低级键盘钩子资料

天天上班很无聊,就是拿CAD制图,很无聊啊,还不能看点自己想看的东西(电子书),公司

领导都象幽灵一样的会飘到你背后!!!!所以写了这么个程序,让软件从文件中读入数据并

写入CAD的标题(领导站着看不到屏幕最上面的标题栏,哈哈哈哈哈……真是服了我自己了)

。另外屏蔽了F1还有Windows键,免得点到弹出一些垃圾信息,打扰工作。呵呵。
Alt+Q推出程序。F1下一个信息,F2上一个信息。
最近我的VC有点问题了,不能编译,郁闷啊。用Dev-C++编译成功。
代码:
/***********************************************************


#include <Windows.h>
#include <winbase.h>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

#define WM_NEXT WM_USER+101          //向前一条信息的消息
#define WM_PRE WM_USER+102           //向后一条信息的消息
#define WM_RESET WM_USER+103         //清零的消息
#define WM_SH WM_USER+104         //显示/隐藏的消息
#define WM_SHTITLE WM_USER+105         //显示/隐藏标题的消息
#define WM_RESETWND WM_USER+106         //重新设置WND

HHOOK hhkLowLevelKybd;//定义低级键盘钩子

bool wndshowing=false;//显示还是隐藏
bool titleshowing=false;
HWND setwnd;//更改标题的窗口的wnd
HWND shwnd;//更改显示隐藏状态的窗口的wnd
HWND softwnd;//本软件自己的窗口wnd
int maxrow=0;//文件中的最大行数
int showrow=0;//要显示的行
char bookname[50];//存储要读取的文件名
vector <string> bk;//用来存储文件的结构,每个结构一行信息
char showing[256];//更改的标题,对应于bk中的一个成员
char temp[2];
char cfgpath[100];

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/
//钩子函数实现屏蔽系统按键
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
{
 BOOL fEatKeystroke = FALSE;

 if (nCode == HC_ACTION) {
  switch (wParam) {
  case case    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
   if(p->vkCode==VK_F1&&wParam==WM_KEYUP)//如果是F1发送消息通知下一个
    PostMessage(softwnd,WM_NEXT,0,0);
   if(p->vkCode==VK_F2&&wParam==WM_KEYUP)//如果是F2发送消息通知上一个
    PostMessage(softwnd,WM_PRE,0,0);
            if(p->vkCode==VK_F4&&wParam==WM_KEYUP)//如果是F4发送消息通知WND清零
    PostMessage(softwnd,WM_RESETWND,0,0);
   if(p->vkCode==VK_F12&&wParam==WM_KEYUP)//如果是F12发送消息通知重新读入文件
    PostMessage(softwnd,WM_RESET,0,0);
   if(p->vkCode==VK_ESCAPE&&wParam==WM_KEYUP)//如果是ESC发送消息通知清除TITLE
    PostMessage(softwnd,WM_SHTITLE,0,0);
   if(p->vkCode==VK_LWIN&&wParam==WM_KEYUP)//如果是LWIN发送消息通知切换显示/隐


    PostMessage(softwnd,WM_SH,0,0);
   fEatKeystroke = //屏蔽掉一些系统按键,免的按的时候影响工作^_^
    //((p->vkCode == VK_TAB) && ((p->flags & LLKHF_ALTDOWN) != 0)) ||
    //((p->vkCode == VK_ESCAPE) &&((p->flags & LLKHF_ALTDOWN) != 0)) ||
    //((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) &0x8000) != 0)) 

||
                p->vkCode ==VK_LWIN ||
    p->vkCode ==VK_RWIN ||
    p->vkCode ==VK_APPS ||
    p->vkCode ==VK_F1 ||
                p->vkCode ==VK_F2 ||
    p->vkCode ==VK_F4 ;

   break;
  }
 }
 return(fEatKeystroke lParam));
}

/*  Make the class name into a global variable  */
char szClassName[ ] = "Cigar's HotKey";

int WINAPI WinMain (HINSTANCE hThisInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpszArgument,
     int nFunsterStil)

{
 HWND hwnd;               /* This is the handle for our window */
 MSG messages;            /* Here messages to the application are saved */
 WNDCLASSEX wincl;        /* Data structure for the windowclass */

 /* The Window structure */
 wincl.hInstance = hThisInstance;
 wincl.lpszClassName = szClassName;
 wincl.lpfnWndProc = WindowProcedure;      /* This function is called by

windows */
 wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
 wincl.cbSize = sizeof (WNDCLASSEX);

 /* Use default icon and mouse-pointer */
 wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
 wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
 wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
 wincl.lpszMenuName = NULL;                 /* No menu */
 wincl.cbClsExtra = 0;                      /* No extra bytes after the window

class */
 wincl.cbWndExtra = 0;                      /* structure or the window

instance */
 /* Use Windows's default color as the background of the window */
 wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

 /* Register the window class, and if it fails quit the program */
 if (!RegisterClassEx (&wincl))
  return 0;

 /* The class is registered, let's create the program*/
 hwnd = CreateWindowEx (
  0,                   /* Extended possibilites for variation */
  szClassName,         /* Classname */
  "Cigar's HotKey",       /* Title Text */
  WS_OVERLAPPEDWINDOW, /* default window */
  CW_USEDEFAULT,       /* Windows decides the position */
  CW_USEDEFAULT,       /* where the window ends up on the screen */
  544,                 /* The programs width */
  375,                 /* and height in pixels */
  HWND_DESKTOP,        /* The window is a child-window to desktop */
  NULL,                /* No menu */
  hThisInstance,       /* Program Instance handler */
  NULL                 /* No Window Creation data */
  );

 /* Make the window visible on the screen */
 //ShowWindow (hwnd, nFunsterStil);

 //将hwnd保存到全局变量中,便于在钩子中发送消息
 softwnd=hwnd;
    shwnd=NULL;
    setwnd=NULL;
 //判该程序是否运行
 HANDLE Handle;
 Handle = CreateMutex(NULL, TRUE,"Cigar's HotKey");
 if (Handle == NULL)
 {
  MessageBox(NULL,"注册系统标志失败,程序自动退出!","错误",MB_ICONERROR|MB_OK);
  PostQuitMessage(0);
 }
 if (GetLastError() == ERROR_ALREADY_EXISTS)
 {
  MessageBox(NULL,"系统热键监视器已经在运行了,本次运行自动取消!","提

示",MB_ICONSTOP|MB_OK);
  PostQuitMessage(0);
 }
 //注册热键
 if(!RegisterHotKey(hwnd,3160,MOD_ALT,'Q'))//ALT+Q退出程序
 {
  MessageBox(NULL,"安装退出热键失败,程序退出","Warning",MB_OK);
  PostQuitMessage(0);
 }


 //安装低级按键钩子
 hhkLowLevelKybd  = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,

hThisInstance, 0);
 MessageBox(NULL,"-->已经安装了系统热键<--/n-->ALT+Q退出程序<--/n/n-------->确

定后我会默默的注视你的哦!^_^","提示",MB_OK);

    GetCurrentDirectory(sizeof(cfgpath),cfgpath);
    sprintf(cfgpath,"%s%s",cfgpath,"//config.ini");
 memset(bookname,0,50);
 ::GetPrivateProfileString("SoftTitle","BookName","book.bk",bookname,sizeof

(bookname),cfgpath);
 showrow=::GetPrivateProfileInt("SoftTitle","ShowRow",0,cfgpath);
 //初始化文件
 ifstream winf;
 winf.open(bookname);
 string wstr;
 while(getline(winf, wstr))
  bk.push_back(wstr);
 maxrow=bk.size();
 winf.close();

 /* Run the message loop. It will run until GetMessage() returns 0 */
 while (GetMessage (&messages, NULL, 0, 0))
 {
  /* Translate virtual-key messages into character messages */
  TranslateMessage(&messages);
  /* Send message to WindowProcedure */
  DispatchMessage(&messages);
 }

 /* The program return-value is 0 - The value that PostQuitMessage() gave */
 return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,

LPARAM lParam)
{
 switch (message)                  /* handle the messages */
 {
 case       setwnd=NULL;
        ShowWindow(shwnd,SW_SHOW);
        SetForegroundWindow(shwnd);
        wndshowing=true;
        shwnd=NULL;
  //关闭系统热键
  UnregisterHotKey(hwnd,3160);
  //卸载键盘钩子
  UnhookWindowsHookEx(hhkLowLevelKybd);
  PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  break;
 case if(!IsWindow(setwnd))//如果没有设置窗体wnd这里设置,否则不理
   setwnd=GetForegroundWindow();//取得当前激活的窗体的wnd
  if(showrow<maxrow)//没有到文件最后一行就下一行,否则不动
   showrow++; 
  sprintf(temp,"%d",showrow);
  WritePrivateProfileString("SoftTitle","ShowRow",temp,cfgpath);
  memset(showing,0,256);
  if(showrow==0)
   sprintf(showing,"AutoCAD 2004");
  else
   sprintf(showing,"AutoCAD 2004                  %s",(char *)bk[showrow-

1].c_str());
  SetWindowText(setwnd,showing);
        titleshowing=true;
  break;
 case if(!IsWindow(setwnd))//如果没有设置窗体wnd这里设置,否则不理
   setwnd=GetForegroundWindow();//取得当前激活的窗体的wnd
  if(showrow>0)//不是文件第一行就上一行,否则不动
   showrow--;    
  sprintf(temp,"%d",showrow);
  WritePrivateProfileString("SoftTitle","ShowRow",temp,cfgpath);
  memset(showing,0,256);
  if(showrow==0)
   sprintf(showing,"AutoCAD 2004");
  else
   sprintf(showing,"AutoCAD 2004                  %s",(char *)bk[showrow-

1].c_str());
  SetWindowText(setwnd,showing);
        titleshowing=true;
  break;
 case       if(titleshowing)
        {
        showrow--;    
  sprintf(temp,"%d",showrow);
  WritePrivateProfileString("SoftTitle","ShowRow",temp,cfgpath);
        }
  sprintf(showing,"AutoCAD 2004");
  SetWindowText(setwnd,showing);
        titleshowing=false;  
  break;
 case showrow=0;    
  sprintf(temp,"%d",showrow);
  WritePrivateProfileString("SoftTitle","ShowRow",temp,cfgpath);
     break;
               setwnd=NULL;
            ShowWindow(shwnd,SW_SHOW);
            SetForegroundWindow(shwnd);
            wndshowing=true;
            shwnd=NULL;
        break;
 case if(wndshowing&&IsWindow(shwnd))
  {
        ShowWindow(shwnd,SW_HIDE);
        wndshowing=false;
        }
        else if(!wndshowing&&IsWindow(shwnd))
        {
             ShowWindow(shwnd,SW_SHOW);
             SetForegroundWindow(shwnd);
             wndshowing=true;
             }
        else
        {
            shwnd=GetForegroundWindow();
            ShowWindow(shwnd,SW_HIDE);
            wndshowing=false;
            }             
  break;
 case if(wParam==3160)//BYE BYE
  {
   if(IDNO==MessageBox(NULL,"真的要走了么?","????",MB_ICONQUESTION|4))
                return 0;
   //关闭系统热键
   UnregisterHotKey(hwnd,3160);
            setwnd=NULL;
            ShowWindow(shwnd,SW_SHOW);
            SetForegroundWindow(shwnd);
            wndshowing=true;
            shwnd=NULL;
   //卸载键盘钩子
   UnhookWindowsHookEx(hhkLowLevelKybd);
   PostQuitMessage (0);
   break;
  } 

 default:                      /* for messages that we don't deal with */
  return DefWindowProc (hwnd, message, wParam, lParam);
 }

 return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值