Postby stephb7 » Thu Jan 12, 2006 20:10
Here's my code to convert a DirectInput key into a CEGUI utf value. It sort of works.
CEGUI::utf32 keycodeToUTF32( UINT keyCode)
{
CEGUI::utf32 utf = 0;
// Retrieve the keyboard layout in order to perform the necessary convertions
HKL hklKeyboardLayout = GetKeyboardLayout(0); // 0 means current thread
if(hklKeyboardLayout == 0)
return utf;
// Retrieve the keyboard state
BYTE keyboardState[256];
if (GetKeyboardState(keyboardState) == FALSE)
return utf;
/* 0. Convert virtual-key code into a scan code
1. Convert scan code into a virtual-key code
Does not distinguish between left- and right-hand keys.
2. Convert virtual-key code into an unshifted character value
in the low order word of the return value. Dead keys (diacritics)
are indicated by setting the top bit of the return value.
3. Windows NT/2000/XP: Convert scan code into a virtual-key
Distinguishes between left- and right-hand keys.*/
UINT virtualKey = MapVirtualKeyEx(keyCode, 3, hklKeyboardLayout);
if (virtualKey == 0) // No translation possible
return utf;
/* 0 means that there is no menu active
Return values:
0. No translation available
1. A translation exists
2. Dead-key in buffer[1] could not be combined with character
The value in buffer[0] is invalid */
unsigned char buffer[2];
if (ToAsciiEx(virtualKey, keyCode, keyboardState, (LPWORD) buffer, 0, hklKeyboardLayout) == 1)
utf = buffer[0];
return utf;
}
When I place my keyboard in French Canada mode the / key now generates a é. My function correctly converts the DirectInput scancode into the utf32 value of 233, which corresponds to E9 in hexadecimal, the "Latin Small Letter E with Acute" according to Character Map (charmap.exe) which comes with Windows, with the Arial font.
The problem is that I'm unable to view this letter. I'm setting the font to Arial with the following:
mGUISystem = new CEGUI::System(mGUIRenderer, (const CEGUI::utf8*) "Player_Data/Logs/CEGUI_Interface.log");
CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative); // Standard, Errors, Informative, Insane
CEGUI::Font* guiFont = CEGUI::FontManager::getSingletonPtr()->createFont("Arial", "c:/windows/fonts/arial.ttf", 12, CEGUI::FontFlag::Default);
mGUISystem->setDefaultFont(guiFont);
And creating the text box as:
CEGUI::Editbox* editBox = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Editbox", (CEGUI::utf8*)"Editor");
mEditorGuiSheet->addChildWindow(editBox);
editBox->setPosition(CEGUI::Point(0.35f, 0.55f));
editBox->setSize(CEGUI::Size(0.3f, 0.1f));
editBox->setFont("Arial");
And injecting the characters with:
CEGUI::utf32 utf = keycodeToUTF32(arg.key);
CEGUI::System::getSingleton().injectChar(utf);
arg.key contains the value of 0x35, DIK_SLASH. Is there something else I'm supposed to do?