KeyCount.docx
- 文档编号:4642904
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:10
- 大小:18.33KB
KeyCount.docx
《KeyCount.docx》由会员分享,可在线阅读,更多相关《KeyCount.docx(10页珍藏版)》请在冰豆网上搜索。
KeyCount
KybdHk.c
/****************************************************************
Modulename:
KybdHk.c
Writtenby:
JonathanLocke
Notices:
Copyright(c)1995JeffreyRichter
Purpose:
Dllwhichsetsasystemkeyboardhook.
****************************************************************/
#include"Win95ADG.h"/*SeeAppendixAfordetails*/
#include
#include
#include
#pragmawarning(disable:
4001)/*Singlelinecomment*/
//WemustdefineKYBDHKLIBAPIas'dllexport'beforeincludingKybdHk.h.
//KybdHk.hwillseethatwehavealreadydefinedKYBDHKLIBAPIand
//willnot(re)defineitas'dllimport'.
#defineKYBDHKLIBAPI__declspec(dllexport)
#include"KybdHk.h"
/////////////////////////////////////////////////////////////////
HINSTANCEg_hinstDll=NULL;//KybdHk.dll'sinstancehandle
/////////////////////////////////////////////////////////////////
//CallingSetWindowsHookExwithathreadIdofzerowillcausethisDLLtobe
//injectedintootherprocesses.Therefore,wemustdeclareashareddata
//sectionsothatallmappingsofourDLL(inallprocesses)sharethesame
//setofglobalvariables.
//#pragmacomment(lib,"kernel32""-section:
Shared,rws")
#pragmadata_seg("Shared")
HHOOKg_hhook=NULL;//Hookhandleforsystem-widekeyboardhook
HWNDg_hwndPost=NULL;//Windowtonotifyofkeyboardevents
UINTg_uMsgNotify=WM_NULL;//Notificationmessagetoposttog_hwndPost
HWNDg_hwndEdit=NULL;//Characterstorejectwithhook
#pragmadata_seg()
//#pragmacomment(linker"/section:
Shared,rws")
/////////////////////////////////////////////////////////////////
staticLRESULTWINAPIKeyboardHook_HookProc(intnCode,
WPARAMwParam,LPARAMlParam){
LRESULTlResult=CallNextHookEx(g_hhook,nCode,wParam,lParam);
if(nCode==HC_ACTION){
TCHARac[2];
BYTEbKeyState[256];
UINTuScanCodeAndShiftState=HIWORD(lParam)&(0x00FF|KF_UP);
//Notifyapplication'swindowthatthisthreadreceivedakeystroke
PostMessage(g_hwndPost,g_uMsgNotify,wParam,lParam);
//TranslatekeystroketoANSIorUnicodeandrejectitifit'sone
//ofthecharactersing_hwndEdit.
GetKeyboardState(bKeyState);
#ifdefUNICODE
if(ToUnicode(wParam,uScanCodeAndShiftState,bKeyState,ac,1,0)==1){
#else
if(ToAscii(wParam,uScanCodeAndShiftState,bKeyState,(PWORD)ac,0)==1){
#endif
TCHARsz[256];
FORWARD_WM_GETTEXT(g_hwndEdit,adgARRAY_SIZE(sz),sz,SendMessage);
if(_tcschr(sz,ac[0])){
//Rejectthekeyevent.
lResult=1;
}
}
}
return(lResult);
}
/////////////////////////////////////////////////////////////////
BOOLWINAPIKeyboardHook_Start(HWNDhwndPost,UINTuMsgNotify,HWNDhwndEdit){
HHOOKhhook;
//ReturnFALSEifhookhasalreadybeeninstalled.
if(g_hhook!
=NULL)
return(FALSE);
adgASSERT(IsWindow(hwndPost));
g_hwndPost=hwndPost;
g_uMsgNotify=uMsgNotify;
g_hwndEdit=hwndEdit;
//Giveuptheremainderofourthread'stimeslice.
//Thisgivesusabetterchanceofgettingallthewaythroughthecall
//toSetWindowsHookExandthevariableassignmenttog_hhookinoneshot.
//Ifwearepreemptedafterthehookisset,butbeforethevariableis
//updated,itispossibleforanotherthreadtoenterourhookfilter
//functionbeforethehookhandleisvalid.UnderWindowsNTthisisnot
//aproblem.UnderWindows95,nothavingavalidhookhandlewillcause
//CallNextHookExtofail.Ifthereissomereasonthatitiscritical
//thatyourapplicationsucceedincallingthenextfilterfunctionin
//thechain,theonlyrobustwaytowritethiscodeistousesomething
//liketheSWMRG(single-writer,multiple-readerguard)objectdeveloped
//inAdvancedWindows(MicrosoftPress).
Sleep(0);
//Setourkeyboardhook.
hhook=SetWindowsHookEx(WH_KEYBOARD,
KeyboardHook_HookProc,g_hinstDll,0);
//Ensurethatg_hhookisalwaysvalid(evenifwearepreemptedwhilst
//inthemiddleofwritingtoit)byupdatingthevariableatomically.
InterlockedExchange((PLONG)&g_hhook,(LONG)hhook);
return(g_hhook!
=NULL);
}
/////////////////////////////////////////////////////////////////
BOOLWINAPIKeyboardHook_Stop(){
BOOLfOK=TRUE;
//Onlyuninstallthehookifitwassuccessfullyinstalled.
if(g_hhook!
=NULL){
fOK=UnhookWindowsHookEx(g_hhook);
g_hhook=NULL;
}
return(fOK);
}
/////////////////////////////////////////////////////////////////
BOOLWINAPIDllMain(HINSTANCEhinstDll,DWORDfdwReason,LPVOIDlpvReserved){
switch(fdwReason){
caseDLL_PROCESS_ATTACH:
g_hinstDll=hinstDll;
break;
}
return(TRUE);
}
KeyCount.c
/***************************************************************
Modulename:
KeyCount.c
Writtenby:
JonathanLocke
Notices:
Copyright(c)1995JeffreyRichter
Purpose:
Keyboardhooksdemonstrationapplication
****************************************************************/
#include"Win95ADG.h"/*SeeAppendixAfordetails*/
#include
#include
#pragmawarning(disable:
4001)/*Singlelinecomment*/
#include
#include"KybdHk.h"
#include"KeyCount.h"
#include"Resource.h"
#pragmawarning(disable:
4001)/*Singlelinecomment*/
/////////////////////////////////////////////////////////////////
//ForcealinkwiththeimportlibraryforKybdHk.dll
#pragmacomment(lib,adgLIBBUILDTYPEadgLIBCPUTYPE"\\""KybdHk")
/////////////////////////////////////////////////////////////////
#defineGETKEYCOUNT(hwnd)((UINT)GetWindowLong(hwnd,GWL_USERDATA))
#defineSETKEYCOUNT(hwnd,c)SetWindowLong(hwnd,GWL_USERDATA,(UINT)c)
/////////////////////////////////////////////////////////////////
BOOLKeyCount_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam){
adgSETDLGICONS(hwnd,IDI_KEYCOUNT,IDI_KEYCOUNT);
//Makethekeycountdialogtopmostsothatitiseasytowatchkeysin
//applicationsthattakeupmostorallofthescreen.
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
EnableWindow(GetDlgItem(hwnd,IDC_START),TRUE);
EnableWindow(GetDlgItem(hwnd,IDC_STOP),FALSE);
//Resetkeystrokecount.
SETKEYCOUNT(hwnd,0);
return(TRUE);//Acceptdefaultfocuswindow.
}
/////////////////////////////////////////////////////////////////
voidKeyCount_OnKeyEvent(HWNDhwnd,UINTvk,LPARAMlParam){
BOOLfDown=((HIWORD(lParam)&KF_UP)==0);
TCHARsz[128];
UINTuKeyCount=GETKEYCOUNT(hwnd);
TCHARc=(TCHAR)LOWORD(MapVirtualKey(vk,2));
if(fDown)
uKeyCount++;
SetDlgItemInt(hwnd,IDC_KEYCOUNT,uKeyCount,FALSE);
wsprintf(sz,__TEXT("char=%c,virtualkey=%d<%s>"),
IsCharAlphaNumeric(c)?
c:
__TEXT('?
'),vk,
fDown?
__TEXT("pressed"):
__TEXT("released"));
SetDlgItemText(hwnd,IDC_LASTKEY,sz);
SETKEYCOUNT(hwnd,uKeyCount);
}
/////////////////////////////////////////////////////////////////
voidKeyCount_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify){
HWNDhwndEdit;
switch(id){
caseIDCANCEL:
//Allowsdialogboxtoclose.
//Unhookkeyboardhookbeforeexiting.
if(!
KeyboardHook_Stop())
adgMB(__TEXT("Unabletostopkeyboardhook"));
EndDialog(hwnd,id);
break;
caseIDC_START:
hwndEdit=GetDlgItem(hwnd,IDC_EDIT);
if(KeyboardHook_Start(hwnd,UM_KEYEVENT,hwndEdit)){
SetDlgItemText(hwnd,IDC_LASTKEY,__TEXT(""));
SetDlgItemInt(hwnd,IDC_KEYCOUNT,0,FALSE);
SETKEYCOUNT(hwnd,0);
EnableWindow(GetDlgItem(hwnd,IDC_START),FALSE);
EnableWindow(GetDlgItem(hwnd,IDC_STOP),TRUE);
SetFocus(GetDlgItem(hwnd,IDC_STOP));
}else{
adgMB(__TEXT("Unabletostartkeyboardhook"));
}
break;
caseIDC_STOP:
if(!
KeyboardHook_Stop())
adgMB(__TEXT("Unabletostopkeyboardhook"));
EnableWindow(GetDlgItem(hwnd,IDC_START),TRUE);
EnableWindow(GetDlgItem(hwnd,IDC_STOP),FALSE);
SetFocus(GetDlgItem(hwnd,IDC_START));
break;
}
}
////////////////////////////////////////////////////////////////
BOOLWINAPIKeyCount_DlgProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam){
switch(uMsg){
//StandardWindowsmessages
adgHANDLE_DLGMSG(hwnd,WM_INITDIALOG,KeyCount_OnInitDialog);
adgHANDLE_DLGMSG(hwnd,WM_COMMAND,KeyCount_OnCommand);
//Keyboardhooknotificationmessage
adgHANDLE_DLGMSG(hwnd,UM_KEYEVENT,KeyCount_OnKeyEvent);
}
return(FALSE);//Wedidn'tprocessthemessage.
}
///////////////////////////////////////////////////////////////
intWINAPIWinMain(HINSTANCEhinstExe,HINSTANCEhinstPrev,
LPSTRlpszCmdLine,intnCmdShow){
adgWARNIFUNICODEUNDERWIN95();
adgVERIFY(-1!
=DialogBox(hinstExe,MAKEINTRESOURCE(IDD_KEYCOUNT),
NULL,KeyCount_DlgProc));
return(0);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- KeyCount