读书人

MFC 接受键盘输入,该怎么处理

发布时间: 2012-03-24 14:00:46 作者: rapoo

MFC 接受键盘输入 - C++ Builder / Windows SDK/API
各位鸟,大家好,请问大家一下,如何在 MFC 中, 接收键盘输入的字符?本人在先恭候。

[解决办法]

C/C++ code
public:      virtual BOOL PreTranslateMessage(MSG* pMsg);//-------------------------BOOL CTestKeyboardDlg::PreTranslateMessage(MSG* pMsg) {    m_keys.PreTranslateKey();    CString str;    if(m_keys.GetKeyAllState(str))    {        AfxMessageBox(str);    }    // TODO: Add your specialized code here and/or call the base class        return CDialog::PreTranslateMessage(pMsg);}
[解决办法]
C/C++ code
上面的代码加上KeyManage m_keys;//KeyManage类部分// KeyManage.h: interface for the KeyManage class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_KEYMANAGE_H__0ED0B8CE_1D30_4E77_BCD3_C247AFAB9B2A__INCLUDED_)#define AFX_KEYMANAGE_H__0ED0B8CE_1D30_4E77_BCD3_C247AFAB9B2A__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class KeyManage  {public:    BOOL GetKeyAllState(CString &str);    int GetKeyState(UINT vKey);    void PreTranslateKey();    KeyManage();    virtual ~KeyManage();    BYTE m_key[256];};#endif // !defined(AFX_KEYMANAGE_H__0ED0B8CE_1D30_4E77_BCD3_C247AFAB9B2A__INCLUDED_)#include "stdafx.h"#include "KeyManage.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////KeyManage::KeyManage(){    memset(m_key,0,sizeof(BYTE)*256);}KeyManage::~KeyManage(){}void KeyManage::PreTranslateKey(){    GetKeyboardState(m_key);}inline int KeyManage::GetKeyState(UINT vKey){    ASSERT(vKey<256);    return m_key[vKey]&0x80;}BOOL KeyManage::GetKeyAllState(CString &str){    BOOL bPress = FALSE;    str = "";    for(int i=0;i<256;i++)    {        if(GetKeyState(i))        {            if(!bPress)            {                bPress = true;            }            char c = MapVirtualKey(i,2);            str += c;        }    }    return bPress;}
[解决办法]

家里网络 太差了
贴了几次都不成功

读书人网 >C++ Builder

热点推荐