#pragma region CPL License
/*
Nuclex Native Framework
Copyright (C) 2002-2013 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion // CPL License
// If the library is compiled as a DLL, this ensures symbols are exported
#define NUCLEX_INPUT_SOURCE 1
#include "Nuclex/Input/Config.h"
#if defined(NUCLEX_INPUT_WIN32)
#include "WindowMessageKeyboard.h"
namespace {
// ------------------------------------------------------------------------------------------- //
/// Converts a virtual key code to an entry in the key enumeration
/// Virtual key code that will be converted
/// Extended informations about the affected key
/// The enumeration value equivalent to the virtual key code
Nuclex::Input::Devices::Key::Enum KeyFromVirtualKey(int virtualKeyCode, int extended) {
using namespace Nuclex::Input::Devices;
switch(virtualKeyCode) {
case VK_RETURN: {
bool isMain = (extended & 0x01000000) != 0;
if(isMain) {
return Key::Return;
} else {
return Key::Enter;
}
break;
}
case VK_CONTROL: {
bool isRight = (extended & 0x01000000) != 0;
if(isRight) {
return Key::RightControl;
} else {
return Key::LeftControl;
}
break;
}
case VK_MENU: {
bool isRight = (extended & 0x01000000) != 0;
if(isRight) {
return Key::RightAlt;
} else {
return Key::LeftAlt;
}
break;
}
case VK_SHIFT: {
virtualKeyCode = ::MapVirtualKeyA(
(extended & 0x00FF0000) >> 16, MAPVK_VSC_TO_VK_EX
);
} // intentional fall-through
default: {
return static_cast(virtualKeyCode);
}
}
}
// ------------------------------------------------------------------------------------------- //
} // anonymous namespace
namespace Nuclex { namespace Input { namespace Devices {
// ------------------------------------------------------------------------------------------- //
WindowMessageKeyboard::WindowMessageKeyboard() {}
// ------------------------------------------------------------------------------------------- //
WindowMessageKeyboard::~WindowMessageKeyboard() {}
// ------------------------------------------------------------------------------------------- //
bool WindowMessageKeyboard::ProcessWindowMessage(
UINT message, WPARAM parameter1, LPARAM parameter2
) {
switch(message) {
// These seem to be generated when the user holds down the left alt key
case WM_SYSKEYDOWN: {
int virtualKeyCode = static_cast(parameter1);
bool dontHandle =
(virtualKeyCode == VK_F4) ||
(virtualKeyCode == VK_ESCAPE) ||
(virtualKeyCode == VK_SPACE);
if(dontHandle) {
return false;
}
} // intentional fall-through
// A key has been pressed down. Repeated at the keyboard repeat rate if
// the use continues to hold the key down.
case WM_KEYDOWN: {
int virtualKeyCode = static_cast(parameter1);
int extended = static_cast(parameter2);
BufferedKeyboard::BufferKeyPress(KeyFromVirtualKey(virtualKeyCode, extended));
break;
}
// A key has been released again
case WM_SYSKEYUP: case WM_KEYUP: {
int virtualKeyCode = static_cast(parameter1);
int extended = static_cast(parameter2);
BufferedKeyboard::BufferKeyRelease(KeyFromVirtualKey(virtualKeyCode, extended));
break;
}
// Receives the fully translated character (so we don't have to care about
// keyboard layouts and dead keys) if the user enters something.
case WM_CHAR: {
wchar_t character = static_cast(parameter1);
BufferedKeyboard::BufferCharacterEntry(character);
break;
}
// Application has gained or lost focus
case WM_ACTIVATE: {
// TODO: Raise all keys that are down if
} // Intentional fall-through
// Any other window messages are ignored
default: {
return false; // unhandled
}
}
return true; // handled
}
// ------------------------------------------------------------------------------------------- //
const std::string &WindowMessageKeyboard::GetName() const {
static std::string name("Default Windows Keyboard");
return name;
}
// ------------------------------------------------------------------------------------------- //
}}} // namespace Nuclex::Input::Devices
#endif // defined(NUCLEX_INPUT_WIN32)