开发者

How to convert virtual key code to character code?

In the onkeydown() handler I am getting 219 as the keycode for '['; however, the actual character valu开发者_运维问答e of '[' is 91. Is there any way to map these two?


If you are using Windows, you should look into the ToUnicodeEx function.


step 1: Open VC++ 6.0
Step 2: File --> New --> Projects --> Win32 Application
Give your project name
Step 3: File --> New --> Files --> C++ Source File
Give your file name
step 4: In your CPP file

/* Mfc program to handle virtual key codes. */
#include<afxwin.h>
class myframe : public CFrameWnd
{
public: 
    myframe()
    {
        Create(0,"Menu Program");
    }
    void OnKeyDown(UINT n)
    {
        switch(n)
        {
        case VK_LEFT:
            MessageBox("Left Arrow","Hellow");
            break;
        case VK_RIGHT:
            MessageBox("Right Arrow","Hellow");
            break;
        case VK_UP:
            MessageBox("Up Arrow","Hellow");
            break;
        case VK_DOWN:
            MessageBox("Down Arrow","Hellow");
            break;
        case VK_NUMPAD0:
            MessageBox("Number ZERO","Hellow");
            break;
        case VK_NUMPAD9:
            MessageBox("Number NINE","Hellow");
            break;
        case VK_SPACE:
            MessageBox("Space Bar","Hellow");
            break;
        case VK_BACK:
            MessageBox("BACK KEY","Hellow");
            break;
        case VK_SHIFT:
            MessageBox("SHIFT KEY","Hellow");
            break;
        }
    }       
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
class myapp : public CWinApp
{
public:
    int InitInstance()
    {
        m_pMainWnd=new myframe();
        m_pMainWnd->ShowWindow(3);      
        return 1;
    }
};
myapp app;

step 5: Project --> settings --> Choose MFC as shared DLL
Step 6: Bulid --> Compile
step 7: Build --> Build
Step 8: Build --> Execute


MapVirtualKey() is also useful.


You can try using the MapVirtualKey(UINT uCode,UINT uMapType) function that is built into the Windows.h library.

An example would be:

// Converts 0x1B (escape key) to the unshifted character value 27
MapVirtualKey( 0x1B, MAPVK_VK_TO_CHAR); 

OR

if (GetAsyncKeyState(VK_SHIFT) & (1 << 16)) {
    std::cout << MapVirtualKey(VK_SHIFT,MAPVK_VK_TO_CHAR);
}

You can read more at MapVirtualKeyA function (winuser.h)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜