3.3 Using the Input Core

来源:互联网 发布:android 直播sdk 知乎 编辑:程序博客网 时间:2024/05/23 14:24
 

3.3 Using the Input Core

使用Input Core是非常简单的,只需实例化一个cInput类对象,并列出你想使用的设备的cInputDevice对象,并初始化他们就可是开用了。比如,你说你想使用两个设备,键盘和鼠标:

cInput g_Input; // 全局声明

cInputDevice g_Keyboard;

cInputDevice g_Mouse;

//初始化输入系统(必须的)

// 如果 hWnd hInst 已经被初始化过了

// hWnd = window handle, hInst = instance handle

g_Input.Init(hWnd, hInst);

 

// 创建键盘和鼠标设备

// 使用DirectInput 读取鼠标的方法

g_Keyboard.Create(&g_Input, KEYBOARD);

g_Mouse.Create(&g_Input, MOUSE, FALSE);

// 读取设备当前状态

g_Keyboard.Read();

g_Mouse.Read();

// 如果按下 ESC ,显示一条消息

if(g_Keyboard.GetKeyState(KEY_ESC) == TRUE) {

// 要转载Esc键,所以之前用户必须先释放它

// 如果它可以再次被读取

g_Keyboard.SetLock(KEY_ESC, TRUE);

MessageBox(hWnd, ESCAPE, Key Pressed!, MB_OK);

}

// 如果按下鼠标左键,显示坐标

if(g_Mouse.GetPureButtonState(MOUSE_LBUTTON) == TRUE) {

char b[200];

sprintf(b, %ld, %ld, g_Mouse.GetXPos(), g_Mouse.GetYPos());

MessageBox(hWnd, b, Mouse Coordinates, MB_OK);

}

// 释放所有

g_Mouse.Free();

g_Keyboard.Free();

g_Input.Shutdown();