C语言键盘控制问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言键盘控制问题相关的知识,希望对你有一定的参考价值。

ch=bioskey(0);
switch(ch)

case 19200
............
............
就是用这种思路,怎么编写出一个用方向键盘控制光标,按ESC让程序终止
谢谢拉

用bioskey()函数可以实现读取键盘的上下左右键

函数原型:int bioskey (int cmd)
说 明:bioskey()的函数原型在bios.h中,即必须包含头文件#include<bios.h>

读取方向键可以用下面的程序啊!

#include<stdio.h>
#include<bios.h>
#define Key_Up 0x4800 // 向上方向键   
#define Key_Down 0x5000 // 向下方向键
#define Key_Right 0x4d00 // 向右方向键
#define Key_Left 0x4b00 // 向左方向键
void main()

int key=bioskey(0);   
switch(key)   

  case Key_Up : printf(" 向上方向键被按下"); break;  
  case Key_Down : printf(" 向下方向键被按下"); break;
   case Key_Left : printf(" 向左方向键被按下"); break;
   case Key_Right : printf(" 向右方向键被按下"); break;   

参考技术A TurboC 方向键控制光标移动,ESC 退出

#include "dos.h"
#include "stdio.h"
#include "conio.h"

#define ESC 0x11B
#define LEFT 0x4B00
#define RIGHT 0x4D00
#define UP 0x4800
#define DOWN 0x5000

int main(void)

int ch;
int x, y;

while(1)

ch = bioskey(0);
x = wherex();
y = wherey();
switch(ch)

case ESC:
return 0;
case LEFT:
if (x > 1)
gotoxy(x - 1, y);
break;
case RIGHT:
if (x < 80)
gotoxy(x + 1, y);
break;
case UP:
if (y > 1)
gotoxy(x, y - 1);
break;
case DOWN:
if (y < 25)
gotoxy(x, y + 1);
break;
default:
;


参考技术B TurboC 方向键控制光标移动,ESC 退出

#include "dos.h"
#include "stdio.h"
#include "conio.h"

#define ESC 0x11B
#define LEFT 0x4B00
#define RIGHT 0x4D00
#define UP 0x4800
#define DOWN 0x5000

int main(void)

int ch;
int x, y;

while(1)

ch = bioskey(0);
x = wherex();
y = wherey();
switch(ch)

case ESC:
return 0;
case LEFT:
if (x > 1)
gotoxy(x - 1, y);
break;
case RIGHT:
if (x < 80)
gotoxy(x + 1, y);
break;
case UP:
if (y > 1)
gotoxy(x, y - 1);
break;
case DOWN:
if (y < 25)
gotoxy(x, y + 1);
break;
default:
;


本回答被提问者和网友采纳
参考技术C 1.在Microsoft Windows 中,键盘和鼠标是两个标准的用户输入源,在一些交叠的操作中通常相互补充使用。当然,鼠标在今天的应用程序中比10年前使用得更为广泛。甚至在一些应用程序中,我们更习惯于使用鼠标,例如在游戏、画图程序、音乐程序,以及Web创览器等程序中就是这样。然而,我们可以不使用鼠标,但绝对不能从一般的PC中拆掉键盘。
  Windows程序获得键盘输入的方式:键盘输入以消息的形式传递给程序的窗口过程。实际上,第一次学习消息时,键盘就是一个明显的例子:消息应该传递给应用程序的信息类型。
  Windows用8种不同的消息来传递不同的键盘事件。这好像太多了,但是(就像我们所看到的一样)程序可以忽略其中至少一半的消息而不会有任何问题。并且,在大多数情况下,这些消息中包含的键盘信息会多于程序所需要的。处理键盘的部分工作就是识别出哪些消息是重要的,哪些是不重要的。
2.键盘基础知识
  虽然应用程序在很多情况下可以通过鼠标实现信息的输入,但到现在为止键盘仍然是PC机中不可替代的重要输入设备。
  用键盘当作输入设备,每当用户按下或释放某一个键时,会产生一个中断,该中断激活键盘驱动程序KEYBOARD.DRV来对键盘中断进行处理。 KEYBOARD.DRV程序会根据用户的不同操作进行编码,然后调用Windows用户模块USER.EXE生成键盘消息,并将该消息发送到消息队列中等候处理。
1.扫描码和虚拟码
  扫描码对应着键盘上的不同键,每一个键被按下或释放时,都会产生一个唯一的扫描码作为本身的标识。扫描码依赖于具体的硬件设备,即当相同的键被按下或释放时,在不同的机器上可能产生不同的扫描码。在程序中通常使用由Windows系统定义的与具体设备无关的虚拟码。在击键产生扫描码的同时,键盘驱动程序KEYBOARD.DRV截取键的扫描码,然后将其翻译成对应的虚拟码,再将扫描码和虚拟码一齐编码形成键盘消息。所以,最后发送到消息队列的键盘消息中,既包含了扫描码又包含了虚拟码。
  经常使用的虚拟码在WINDOWS.H文件中定义,常用虚拟码的数值、常量符号和含义如表所示。

取值(16进制) 常量符号 含义
01 VK_LBUTTON 鼠标左键
02 VK_RBUTTON 鼠标右键
03 VK_CANCEL Break中断键
04 VK_MBUTTON 鼠标中键
05-07 -- 未定义
08 VK_BACK (BackSpace)键
09 VK_TAB Tab键
0A-0B -- 未定义
0C VK_CLEAR Clear键
0D VK_RETURN Enter键
0E-0F -- 未定义
10 VK_SHIFT Shift键
11 VK_CONTROL Ctrl键
12 VK_MENU Alt键
13 VK_PAUSE Pause键
14 VK_CAPTIAL CapsLock键
15-19 -- 汉字系统保留
1A -- 未定义
1B VK_ESCAPE Esc键
1C-1F -- 汉字系统保留
20 VK_SPACE 空格键
21 VK_PRIOR PageUp键
22 VK_NEXT PageDown键
23 VK_END End键
24 VK_HOME Home键
25 VK_LEFT ←(Left Arrow)键
26 VK_UP ↑(Up Arrow)键
27 VK_RIGHT →(Right Arrow)键
28 VK_DOWN ↓(Down Arrow)键
29 VK_SELECT Select键
2A -- OEM保留
2B VK_EXECUTE Execute键
2C VK_SNAPSHOT Print Screen键
2D VK_INSERT Insert键
2E VK_DELETE Delete键
2F VK_HELP Help键
30-39 VK_0-VK_9 数字键0-9
3A-40 -- 未定义
41-5A VK_A-VK_Z 字母键A-Z
5B-5F -- 未定义
60-69 VK_NUMPAD0-VK_NUMPAD9 小键盘数字键0-9
6A VK_MULTIPLY *(乘号)键
6B VK_ADD +(加号)键
6C VK_SEPAPATOR 分隔符键
6E VK_SUBTRACT -(减号)键
6F VK_DECIMAL .(小数点)键
70-87 VK_DIVIDE /(除号)键
88-8F VK_F1-VK_F24 F1-F24功能键
90 VK_NUMBERLOCK Number lock键
91 VK_SCROLL Scroll lock键
92-B9 -- 未定义
BA-C0 -- OEM保留
C1-DA -- 未定义
DB_E4 -- OEM保留
E5 -- 未定义
E6 -- OEM保留
E7-E8 -- 未定义
E9-F5 -- OEM保留
F6-FE -- 未定义

2.输入焦点
  同一时刻,Windows中可能有多个不同的程序在运行,也就是说有多个窗口同时存在。这时,键盘由多个窗口共享,但只有一个窗口能够接收到键盘消息,这个能够接收键盘消息的窗口被称为拥有输入焦点的窗口。
  拥有输入焦点的窗口应该是当前的活动窗口,或者是活动窗口的子窗口,其标题和边框会以高亮度显示,以区别于其他窗口。拥有输入焦点的也可以是图标而不是窗口,此时,Windows也将消息发送给图标,只是消息的格式略有不同。
  窗口过程可以通过发送WM_SETFOCUS和 WM_KILLFOCUS消息使窗体获得或失去输入焦点。程序也可以通过捕获WM_SETFOCUS和WM_KILLFOCUS消息来判断窗体何时获得或失去输入焦点。其中WM_SETFOCUS消息表示窗口正获得输入焦点,WM_ KILLFOCUS消息表示窗口正失去输入焦点。
3.键盘消息
  键盘消息分为系统键消息和非系统键消息。系统键消息是指由Aft键和其他键组合而产生的按键消息。当系统键被按下时产生WM_ SYSKEYDOWN消息,当系统键被释放时产生WM_SYSKEYUP消息。 Aft键与其他键形成的组合键通常用于对程序菜单和系统菜单进行选择,或用于在不同的程序之间进行切换。因此,系统键消息应该交由Windows进行处理,用户所编制的程序一般不处理系统键消息,而是将这些消息交由DefWindowProc函数进行处理。如果用户想对系统键消息进行处理,应该在处理完这些消息后,再将其发送给DefWindowProc函数,使得Windows系统能够正常工作。
  某些击键消息可以被转换成字符消息,例如字母键、数字键等。而有些键只能产生按键消息而没有字符消息,例如 Shift键、Insert键等。消息循环中的 TranslateMessage函数可以实现从击键消息向字符消息的转化。当GetMessage函数捕获一个WM_SYSKEYDOWN消息或 WM_KEYDOWN消息后,TranslateMessage函数判断产生该消息的键是否能够被转换成字符消息,如果能,就将该消息转换成字符消息,再通过DispatchMessape函数将转换后的字符消息发送到消息队列中去。字符消息共有以下四种,如表所示。

字符 系统字符 非系统字符
普通字符 WM_SYSCHAR WM_CHAR
死字符 WM_SYSDEADCHAR WM_DEADCHAR

  其中死字符是由某些特殊键盘上的按键所造成的,Windows一般忽略死字符所产生的消息。
  Windows的消息一般是通过一个MSG结构体变量传送给消息处理函数的。对于键盘消息, MSG结构体变量的各个域中较重要的是lParam域和 wParam域。wParam域用于保存按键的虚拟键代码或字符的ASCII码。对于非字符消息,wParam域保存按键的虚拟健代码;对于字符消息, wParam域不保存字符的ASCII码。lParam域则用于保存击键时产生的附加信息,实际上一个32位的lParam变量被分为六部分,记录了以下相关信息:重复次数、OEM扫描码、扩展键标志、关联键标志、前一击键状态和转换状态。lParam域各位的含义如表所示。

位数 含义
0-15 击键重复次数累加
16-23 OEM扫描码
24 是否为扩展键
25-28 未定义
29 是否便用关联键,及Alt键是否同时按下。
30 前一次击键状态,0表示该键前一次状态为抬起,1表示前一次状态为按下
31 转换状态

  按键的次序不同,产生的消息也不相同。例如,按下并释放1键,读过程依次产生如表所示三条消息。按下1键所产生的消息和wParam的取值

消息 wParam变量取值
WM_KEYDOWN 虚拟码1
WM_CHAR ASCII码“1”
WM_KEYUP 虚拟码1

  如果按下Shift键后再按下1键并释放,则依次产生如表所示的消息。按下 Shift键后按 1健所产生的消息和 wParam的取值

消息 wParam变量取值
WM_KEYDOWN 虚拟码 VK_SHIFT
WM_KEYDOWN 虚拟码 VK_1
WM_CHAR ASCII码 “1”
WM_KEYUP 虚拟码 VK_1
WM_KEYUP 虚拟码 VK_SHIF

键盘应用实例
  下面通过一个应用程序实例来说明在实际编程中如何处理键盘消息。

#include <windows.h>
#include <stdio.h>
// 全局变量
RECT rc; //记录滚屏的矩形区域
int xChar, yChar; //文本输入点坐标
WNDCLASSEX wnd; //窗口类结构变量
char szAppName[] = "键盘消息监视程序"; //窗口类名
//函数声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance,int iCmdShow);
//函数:WinMain
//作用:入口函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)

  MSG msg;
  if(!MyRegisterClass(hInstance))
  
    return FALSE;
  
  
   if(!InitInstance(hInstance,iCmdShow))
  
    return FALSE;
  
  
  while (GetMessage (&msg, NULL, 0, 0))
  
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  
  return msg.wParam;

//函数:ShowKey
//作用:实现在窗口中显示按键信息
void ShowKey (HWND hwnd, int iType,char *szMessage,WPARAM wParam,LPARAM lParam)

  static char *szFormat[2] ="%-14s %3d %c %6u %4d %5s %5s %6s %6s",
                "%-14s %3d %c %6u %4d %5s %5s %6s %6s" ;
  char szBuffer[80];
  HDC hdc;
  ScrollWindowEx(hwnd, 0, -yChar, &rc,&rc,NULL,NULL,SW_INVALIDATE);
  hdc = GetDC (hwnd);
  SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
  TextOut (hdc,
       xChar,
       rc.bottom - yChar,
       szBuffer,
       wsprintf szBuffer,
       szFormat[iType],
       szMessage, //消息
       wParam, //虚拟键代码
       (BYTE) (iType ? wParam :‘ ’),//显示字符值
       LOWORD (lParam), // 重复次数
       HIWORD (lParam) & 0xFF, // OEM键盘扫描码
       //判断是否为增强键盘的扩展键
       (PSTR) (0x01000000 & lParam ? “是” : “否”),
       //判断是否同时使用了ALT键
       (PSTR) (0x20000000 & lParam ? “是” : “否”),
       (PSTR) (0x40000000 & lParam ? “按下” : “抬”),
       //判断前一次击键状
       (PSTR)(0x80000000 & lParam ? “按下” : “抬起”))
       //判断转换状态?
       );
  ReleaseDC (hwnd, hdc); ?
  ValidateRect (hwnd, NULL); ?

//函数:WndProc
//作用:处理主窗口的消息
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

  static char szTop[] ="消息键 字符 重复数 扫描码 扩展码 ALT 前一状态 转换状态";
  static char szUnd[] ="_______ __ ____ _____ ______ ______ ___ _______ ______";
  //在窗口中输出文字作为信息标题
  HDC hdc;
  PAINTSTRUCT ps;
  TEXTMETRIC tm;
  switch (iMsg)
  
    case WM_CREATE://处理窗口创建的消息
    hdc = GetDC (hwnd); //设定字体
    SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)); //检取当前字体的度量数据
    GetTextMetrics (hdc, &tm);
    xChar = tm.tmAveCharWidth;//保存字体平均宽度
    yChar = tm.tmHeight; //保存字体高度
    ReleaseDC (hwnd, hdc);
    rc.top = 3 * yChar / 2;
    return 0;
    case WM_SIZE://处理窗口大小改变的消息
    //窗体改变后保存新的滚屏区域右下角坐标
    rc.right = LOWORD (lParam);
    rc.bottom = HIWORD (lParam);
    UpdateWindow (hwnd);
    return 0;
    case WM_PAINT: //处理窗口重绘消息
    InvalidateRect (hwnd, NULL, TRUE);
    hdc = BeginPaint (hwnd, &ps);
    SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
    SetBkMode (hdc, TRANSPARENT) ;
    TextOut (hdc, xChar, yChar / 2, szTop, (sizeof szTop) - 1) ;
    TextOut (hdc, xChar, yChar / 2, szUnd, (sizeof szUnd) - 1) ;
    EndPaint (hwnd, &ps);
    return 0;
    case WM_KEYDOWN:
    //处理键盘上某一键按下的消息
    ShowKey (hwnd, 0, "WM_KEYDOWN",wParam, lParam);
    return 0;
    case WM_KEYUP:
    //处理键盘上某一按下键被释放的消息
    ShowKey (hwnd, 0, "WM_KEYUP", wParam, lParam);
    return 0;
    case WM_CHAR:
    //处理击键过程中产生的非系统键的可见字符消息
    howKey (hwnd, 1, "WM_CHAR", wParam, lParam);
    return 0;
    case WM_DEADCHAR:
    //处理击键过程中产生的非系统键"死字符"消息
    ShowKey (hwnd, 1, "WM_DEADCHAR", wParam, lParam);
    return 0;
    case WM_SYSKEYDOWN:
    //处理系统键按下的消息
    ShowKey (hwnd, 0, "WM_SYSKEYDOWN",wParam, lParam);
    break;
    case WM_SYSKEYUP:
    //处理系统键抬起的消息
    ShowKey (hwnd, 0, "WM_SYSKEYUP", wParam, lParam);
    break;
    case WM_SYSCHAR://处理系统键可见字符消息
    ShowKey (hwnd, 1, "WM_SYSCHAR", wParam, lParam);
    break;
    case WM_SYSDEADCHAR://处理系统键"死字符"消息
    ShowKey (hwnd, 1, "WM_SYSDEADCHAR", wParam, lParam);
    break;
    case WM_DESTROY:
    //处理结束应用程序的消息
    PostQuitMessage (0);
    return 0;
  
  return DefWindowProc (hwnd, iMsg, wParam, lParam);

//函数:MyRegisterClass
//作用:注册窗口类
BOOL MyRegisterClass(HINSTANCE hInstance)

  wnd.cbSize= sizeof (wnd);
  wnd.style = CS_HREDRAW | CS_VREDRAW;
  wnd.lpfnWndProc = WndProc;
  wnd.cbClsExtra = 0;
  wnd.cbWndExtra = 0;
  wnd.hInstance = hInstance;
  wnd.hIcon = LoadIcon (NULL, IDI_APPLICATION);?
  wnd.hCursor = LoadCursor (NULL, IDC_ARROW);
  wnd.hbrBackground = (HBRUSH)
  GetStockObject (WHITE_BRUSH);
  wnd.lpszMenuName = NULL;
  wnd.lpszClassName = szAppName;
  wnd.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  return RegisterClassEx (&wnd);

//函数:InitInstance
//作用:创建主窗口
BOOL InitInstance(HINSTANCE hInstance,int iCmdShow)

  HWND hwnd;
  hwnd = CreateWindow (szAppName,
             "键盘消息监视程序",
             WS_OVERLAPPEDWINDOW,
             CW_USEDEFAULT,CW_USEDEFAULT,
             CW_USEDEFAULT,CW_USEDEFAULT,
             NULL,NULL,hInstance,NULL
             );
  if(!hwnd)
  
    return FALSE;
  
  ShowWindow (hwnd, iCmdShow);
  UpdateWindow (hwnd);
  return TRUE;

C#用windows Api Hooks 控制鼠标键盘在一个窗体内

其实功能很简单,我也做了一半,现在问题是语言是C#
获取一个窗体的句柄,让后把鼠标控制在此窗体里面(也就是不可以离开此窗体),键盘也一样,最好可以控制ctrl+alt+delete的组合键,我做了下,有难度、、、

源码也行,理论也行。谢谢各位

主要是第一个窗体类.其他的2个类是我封装的WINDOWS API

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

namespace HookDemo

public partial class Form1 : Form

private System.Windows.Forms.Timer monitorTimer = null;
private Kits.Hook.WinHook mouseHook = null;
private IntPtr HWND = IntPtr.Zero;

public Form1()

InitializeComponent();
this.monitorTimer = new Timer();
this.monitorTimer.Tick += new EventHandler(this.TimeProc);
this.monitorTimer.Interval = 1000;
mouseHook = new Kits.Hook.WinHook(Kits.Win32API.HookType.WH_MOUSE_LL);


private void Form1_Load(object sender, EventArgs e)

this.WindowState = FormWindowState.Minimized;
this.monitorTimer.Enabled = true;


private void TimeProc(object sender, EventArgs e)

HWND = Kits.Win32API.WinAPI.FindWindow("Notepad", null);
if (HWND != IntPtr.Zero)

//Kits.Win32API.CSharpRect rect = new Kits.Win32API.CSharpRect();
//Kits.Win32API.WinAPI.GetWindowRect(HWND, ref rect);
//this.Text = string.Format("0,1,2,3",rect.top,rect.left,rect.width,rect.height);
if (HWND == Kits.Win32API.WinAPI.GetForegroundWindow())

mouseHook.SetWindowsHookEx(new Kits.Win32API.HookProc(this.MouseHookProc));

else

if (mouseHook.HHook != 0)

mouseHook.UnhookWindowsHookEx();





private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)

if (wParam == new IntPtr((int)Kits.Win32API.WindowsMessages.WM_MOUSEMOVE))

bool isOut = false;
Kits.Win32API.MouseHookStruct MyMouseHookStruct = (Kits.Win32API.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Kits.Win32API.MouseHookStruct));
Kits.Win32API.CSharpRect rect = new Kits.Win32API.CSharpRect();
Kits.Win32API.WinAPI.GetWindowRect(HWND, ref rect);
if (MyMouseHookStruct.pt.x >= rect.right)
isOut = true;
if (MyMouseHookStruct.pt.x <= rect.left)
isOut = true;
if (MyMouseHookStruct.pt.y <= rect.top)
isOut = true;
if (MyMouseHookStruct.pt.y >= rect.bottom)
isOut = true;

//Cursor.Position = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
if (isOut)
return -1;

return Kits.Win32API.WinAPI.CallNextHookEx(mouseHook.HHook, nCode, wParam, lParam);




using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Kits.Win32API

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

public class WinAPI

[DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

[DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);

[DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("User32.dll")]
public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);

[DllImport("user32")]
public static extern IntPtr GetActiveWindow();

[DllImport("user32")]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32")]
public static extern int GetWindowRect(IntPtr hwnd, ref CSharpRect lpRect);

[DllImport("user32")]
public static extern int SetCaretPos(int x, int y);

[DllImport("user32")]
public static extern int ScreenToClient(IntPtr hwnd, ref POINTAPI lpPoint);

[DllImport("user32")]
public static extern int ClientToScreen(IntPtr hwnd, ref POINTAPI lpPoint);

[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);


public struct POINTAPI

public int x;
public int y;


public struct RECT

public long left;
public long top;
public long right;
public long bottom;


public struct CSharpRect

public int top;
public int left;
public int right;
public int bottom;


public enum HookType : int

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


[StructLayout(LayoutKind.Sequential)]
public class POINT

public int x;
public int y;


[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct

public POINT pt;
public int wHitTestCode;
public int dwExtraInfo;


[StructLayout(LayoutKind.Sequential)]
public class MouseLLHookStruct

public POINT pt;
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;


[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct

public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;


public enum WindowsMessages:int

WM_ACTIVATE = 0x6,
WM_ACTIVATEAPP = 0x1C,
WM_AFXFIRST = 0x360,
WM_AFXLAST = 0x37F,
WM_APP = 0x8000,
WM_ASKCBFORMATNAME = 0x30C,
WM_CANCELJOURNAL = 0x4B,
WM_CANCELMODE = 0x1F,
WM_CAPTURECHANGED = 0x215,
WM_CHANGECBCHAIN = 0x30D,
WM_CHAR = 0x102,
WM_CHARTOITEM = 0x2F,
WM_CHILDACTIVATE = 0x22,
WM_CLEAR = 0x303,
WM_CLOSE = 0x10,
WM_COMMAND = 0x111,
WM_COMPACTING = 0x41,
WM_COMPAREITEM = 0x39,
WM_CONTEXTMENU = 0x7B,
WM_COPY = 0x301,
WM_COPYDATA = 0x4A,
WM_CREATE = 0x1,
WM_CTLCOLORBTN = 0x135,
WM_CTLCOLORDLG = 0x136,
WM_CTLCOLOREDIT = 0x133,
WM_CTLCOLORLISTBOX = 0x134,
WM_CTLCOLORMSGBOX = 0x132,
WM_CTLCOLORSCROLLBAR = 0x137,
WM_CTLCOLORSTATIC = 0x138,
WM_CUT = 0x300,
WM_DEADCHAR = 0x103,
WM_DELETEITEM = 0x2D,
WM_DESTROY = 0x2,
WM_DESTROYCLIPBOARD = 0x307,
WM_DEVICECHANGE = 0x219,
WM_DEVMODECHANGE = 0x1B,
WM_DISPLAYCHANGE = 0x7E,
WM_DRAWCLIPBOARD = 0x308,
WM_DRAWITEM = 0x2B,
WM_DROPFILES = 0x233,
WM_ENABLE = 0xA,
WM_ENDSESSION = 0x16,
WM_ENTERIDLE = 0x121,
WM_ENTERMENULOOP = 0x211,
WM_ENTERSIZEMOVE = 0x231,
WM_ERASEBKGND = 0x14,
WM_EXITMENULOOP = 0x212,
WM_EXITSIZEMOVE = 0x232,
WM_FONTCHANGE = 0x1D,
WM_GETDLGCODE = 0x87,
WM_GETFONT = 0x31,
WM_GETHOTKEY = 0x33,
WM_GETICON = 0x7F,
WM_GETMINMAXINFO = 0x24,
WM_GETOBJECT = 0x3D,
WM_GETTEXT = 0xD,
WM_GETTEXTLENGTH = 0xE,
WM_HANDHELDFIRST = 0x358,
WM_HANDHELDLAST = 0x35F,
WM_HELP = 0x53,
WM_HOTKEY = 0x312,
WM_HSCROLL = 0x114,
WM_HSCROLLCLIPBOARD = 0x30E,
WM_ICONERASEBKGND = 0x27,
WM_IME_CHAR = 0x286,
WM_IME_COMPOSITION = 0x10F,
WM_IME_COMPOSITIONFULL = 0x284,
WM_IME_CONTROL = 0x283,
WM_IME_ENDCOMPOSITION = 0x10E,
WM_IME_KEYDOWN = 0x290,
WM_IME_KEYLAST = 0x10F,
WM_IME_KEYUP = 0x291,
WM_IME_NOTIFY = 0x282,
WM_IME_REQUEST = 0x288,
WM_IME_SELECT = 0x285,
WM_IME_SETCONTEXT = 0x281,
WM_IME_STARTCOMPOSITION = 0x10D,
WM_INITDIALOG = 0x110,
WM_INITMENU = 0x116,
WM_INITMENUPOPUP = 0x117,
WM_INPUTLANGCHANGE = 0x51,
WM_INPUTLANGCHANGEREQUEST = 0x50,
WM_KEYDOWN = 0x100,
WM_KEYFIRST = 0x100,
WM_KEYLAST = 0x108,
WM_KEYUP = 0x101,
WM_KILLFOCUS = 0x8,
WM_LBUTTONDBLCLK = 0x203,
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_MBUTTONDBLCLK = 0x209,
WM_MBUTTONDOWN = 0x207,
WM_MBUTTONUP = 0x208,
WM_MDIACTIVATE = 0x222,
WM_MDICASCADE = 0x227,
WM_MDICREATE = 0x220,
WM_MDIDESTROY = 0x221,
WM_MDIGETACTIVE = 0x229,
WM_MDIICONARRANGE = 0x228,
WM_MDIMAXIMIZE = 0x225,
WM_MDINEXT = 0x224,
WM_MDIREFRESHMENU = 0x234,
WM_MDIRESTORE = 0x223,
WM_MDISETMENU = 0x230,
WM_MDITILE = 0x226,
WM_MEASUREITEM = 0x2C,
WM_MENUCHAR = 0x120,
WM_MENUCOMMAND = 0x126,
WM_MENUDRAG = 0x123,
WM_MENUGETOBJECT = 0x124,
WM_MENURBUTTONUP = 0x122,
WM_MENUSELECT = 0x11F,
WM_MOUSEACTIVATE = 0x21,
WM_MOUSEFIRST = 0x200,
WM_MOUSEHOVER = 0x2A1,
WM_MOUSELAST = 0x20A,
WM_MOUSELEAVE = 0x2A3,
WM_MOUSEMOVE = 0x200,
WM_MOUSEWHEEL = 0x20A,
WM_MOVE = 0x3,
WM_MOVING = 0x216,
WM_NCACTIVATE = 0x86,
WM_NCCALCSIZE = 0x83,
WM_NCCREATE = 0x81,
WM_NCDESTROY = 0x82,
WM_NCHITTEST = 0x84,
WM_NCLBUTTONDBLCLK = 0xA3,
WM_NCLBUTTONDOWN = 0xA1,
WM_NCLBUTTONUP = 0xA2,
WM_NCMBUTTONDBLCLK = 0xA9,
WM_NCMBUTTONDOWN = 0xA7,
WM_NCMBUTTONUP = 0xA8,
WM_NCMOUSEHOVER = 0x2A0,
WM_NCMOUSELEAVE = 0x2A2,
WM_NCMOUSEMOVE = 0xA0,
WM_NCPAINT = 0x85,
WM_NCRBUTTONDBLCLK = 0xA6,
WM_NCRBUTTONDOWN = 0xA4,
WM_NCRBUTTONUP = 0xA5,
WM_NEXTDLGCTL = 0x28,
WM_NEXTMENU = 0x213,
WM_NOTIFY = 0x4E,
WM_NOTIFYFORMAT = 0x55,
WM_NULL = 0x0,
WM_PAINT = 0xF,
WM_PAINTCLIPBOARD = 0x309,
WM_PAINTICON = 0x26,
WM_PALETTECHANGED = 0x311,
WM_PALETTEISCHANGING = 0x310,
WM_PARENTNOTIFY = 0x210,
WM_PASTE = 0x302,
WM_PENWINFIRST = 0x380,
WM_PENWINLAST = 0x38F,
WM_POWER = 0x48,
WM_PRINT = 0x317,
WM_PRINTCLIENT = 0x318,
WM_QUERYDRAGICON = 0x37,
WM_QUERYENDSESSION = 0x11,
WM_QUERYNEWPALETTE = 0x30F,
WM_QUERYOPEN = 0x13,
WM_QUEUESYNC = 0x23,
WM_QUIT = 0x12,
WM_RBUTTONDBLCLK = 0x206,
WM_RBUTTONDOWN = 0x204,
WM_RBUTTONUP = 0x205,
WM_RENDERALLFORMATS = 0x306,
WM_RENDERFORMAT = 0x305,
WM_SETCURSOR = 0x20,
WM_SETFOCUS = 0x7,
WM_SETFONT = 0x30,
WM_SETHOTKEY = 0x32,
WM_SETICON = 0x80,
WM_SETREDRAW = 0xB,
WM_SETTEXT = 0xC,
WM_SETTINGCHANGE = 0x1A,
WM_SHOWWINDOW = 0x18,
WM_SIZE = 0x5,
WM_SIZECLIPBOARD = 0x30B,
WM_SIZING = 0x214,
WM_SPOOLERSTATUS = 0x2A,
WM_STYLECHANGED = 0x7D,
WM_STYLECHANGING = 0x7C,
WM_SYNCPAINT = 0x88,
WM_SYSCHAR = 0x106,
WM_SYSCOLORCHANGE = 0x15,
WM_SYSCOMMAND = 0x112,
WM_SYSDEADCHAR = 0x107,
WM_SYSKEYDOWN = 0x104,
WM_SYSKEYUP = 0x105,
WM_TCARD = 0x52,
WM_TIMECHANGE = 0x1E,
WM_TIMER = 0x113,
WM_UNDO = 0x304,
WM_UNINITMENUPOPUP = 0x125,
WM_USER = 0x400,
WM_USERCHANGED = 0x54,
WM_VKEYTOITEM = 0x2E,
WM_VSCROLL = 0x115,
WM_VSCROLLCLIPBOARD = 0x30A,
WM_WINDOWPOSCHANGED = 0x47,
WM_WINDOWPOSCHANGING = 0x46,
WM_WININICHANGE = 0x1A



using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Kits.Win32API;

namespace Kits.Hook

public class WinHook

private int _hookType = 0;
public int HookType get return this._hookType;

private HookProc _hookProc = null;
public HookProc HookProc get return this._hookProc;

private int _hHook = 0;
public int HHook get return this._hHook;

public WinHook(HookType type)
this._hookType = (int)type;


public bool SetWindowsHookEx(HookProc Proc)
bool Success = false;
if (this._hHook == 0)
this._hookProc = Proc;
_hHook = WinAPI.SetWindowsHookEx(this._hookType, this._hookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
if (this._hHook != 0)
Success = true;

return Success;


public bool UnhookWindowsHookEx()
bool Success = false;
if (this._hHook != 0)

Success = WinAPI.UnhookWindowsHookEx(this._hHook);

return Success;


参考技术A 关注

以上是关于C语言键盘控制问题的主要内容,如果未能解决你的问题,请参考以下文章

用C语言写一个控制鼠标键盘自动点的程序复杂吗

c语言 获取鼠标键盘事件

如何实现C语言中用键盘控制光标移动?

c语言 怎样模拟鼠标键盘操作?

C语言控制台窗口图形界面编程:键盘事件

c语言 键盘长按