KeyCount.docx

上传人:b****6 文档编号:4642904 上传时间:2022-12-07 格式:DOCX 页数:10 大小:18.33KB
下载 相关 举报
KeyCount.docx_第1页
第1页 / 共10页
KeyCount.docx_第2页
第2页 / 共10页
KeyCount.docx_第3页
第3页 / 共10页
KeyCount.docx_第4页
第4页 / 共10页
KeyCount.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

KeyCount.docx

《KeyCount.docx》由会员分享,可在线阅读,更多相关《KeyCount.docx(10页珍藏版)》请在冰豆网上搜索。

KeyCount.docx

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);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1