Misc - Intergrated seperate textual input

This commit is contained in:
dean11 2014-02-21 11:43:05 +01:00
parent 53dc42ffc4
commit ed6f80765e
17 changed files with 1161 additions and 1170 deletions

View File

@ -253,6 +253,7 @@
<ClInclude Include="GameClientState\LobbyState.h" />
<ClInclude Include="GameClientState\C_Object.h" />
<ClInclude Include="GameClientState\Buttons\TextField.h" />
<ClInclude Include="Win32Input.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -0,0 +1,70 @@
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_GUARD_RAW_INPUT_H
#define INCLUDE_GUARD_RAW_INPUT_H
#include "..\InputManager.h"
#include "Win32Keyboard.h"
#include "Win32Mouse.h"
#include <vector>
#define NOMINMAX
#include <Windows.h>
#include <map>
/**
* TODO:
* 1. Cordinate system
* 1.1. Pixel cordinates DONE
* 1.2. 0 - 1 cordinate DONW
* 1.3. Origo in middle of the screen ( -1 to 1 )
*/
//RIDEV_INPUTSINK to receive all keyboard input regardless of focus
//dx = +2*(x/w) -1
//dx = -2*(y/h) +1
namespace Input
{
enum RawInput_Usage
{
RawInput_Usage_pointer = 1,
RawInput_Usage_mouse = 2,
RawInput_Usage_joystick = 4,
RawInput_Usage_gamepad = 5,
RawInput_Usage_keyboard = 6,
RawInput_Usage_keypad = 7,
RawInput_Usage_multiAxisController = 8,
RawInput_Usage_TabletPCcontrols = 9,
};
class Win32Input :public InputManager
{
public:
Win32Input();
virtual~Win32Input();
InputObject* CreateDevice(const Enum::SAIType inputType, Typedefs::WindowHandle targetApplication) override;
void ToggleInputSystem(bool enable) override;
void Destroy() override;
private:
std::vector<Win32Keyboard*> keyboard;
std::vector<Win32Mouse*> mouse;
bool enabled;
bool exclusive;
HWND targetHwin;
private:
static Win32Input* instance;
static LRESULT RawInputParser(HWND h, LPARAM l);
static LRESULT CALLBACK RawWindowCallback(HWND h, UINT m, WPARAM w, LPARAM l);
static void WindowActivate(bool activate);
};
}
#endif

View File

@ -21,6 +21,7 @@ namespace Input
{
SAIType_Keyboard,
SAIType_Mouse,
SAIType_ApplicationKeyboard,
SAIType_futureExample1,
SAIType_futureExample2,
SAIType_futureExample3,
@ -36,22 +37,14 @@ namespace Input
/*********************************************************************/
namespace Struct
{
struct SAIPointInt2D
struct SAIPoint2D
{
int x;
int y;
SAIPointInt2D() :x(0), y(0) { }
SAIPointInt2D(int _x, int _y) :x(_x), y(_y) { }
SAIPoint2D() :x(0), y(0) { }
SAIPoint2D(int _x, int _y) :x(_x), y(_y) { }
int Length() { return (abs(x) + abs(y)); }
};
struct SAIPointFloat2D
{
float x;
float y;
SAIPointFloat2D() :x(0.0f), y(0.0f) { }
SAIPointFloat2D(float _x, float _y) :x(_x), y(_y) { }
float Length() { return (fabs(x) + fabs(y)); }
};
struct InputData;
}

View File

@ -38,6 +38,8 @@ namespace Input
* @return Returns a handle to a device that can be rethrown to a specific device.
*/
virtual InputObject* CreateDevice ( const Enum::SAIType inputType, Typedefs::WindowHandle targetApplication = 0 ) = 0;
virtual Keyboard* CreateKeyboardDevice ( Typedefs::WindowHandle targetApplication = 0 ) { return (Keyboard*)CreateDevice(Enum::SAIType_Keyboard, targetApplication); }
virtual Mouse* CreateMouseDevice ( Typedefs::WindowHandle targetApplication = 0 ) { return (Mouse*)CreateDevice(Enum::SAIType_Mouse, targetApplication); }
/** Enables or Disables the Input proccessing.
* @param The toggler.

View File

@ -13,8 +13,9 @@ namespace Input
public:
inline Enum::SAIType Type() { return type; }
virtual inline void Enable () { isEnabled = true; };
virtual inline void Disable () { isEnabled = false; };
virtual void Activate () = 0;
virtual void Deactivate () = 0;
virtual bool IsActive() = 0;
protected:
InputObject(Enum::SAIType type) { this->type = type; }
@ -22,9 +23,6 @@ namespace Input
private:
Enum::SAIType type;
protected:
bool isEnabled;
};
}

View File

@ -133,9 +133,9 @@ namespace Input
//-----------------------------------------------------------------------------------------------------------------------------
namespace Typedefs
{
typedef void(*OnKeyPressCallback)(Enum::SAKI key, const wchar_t[40], Keyboard* sender);
typedef void(*OnKeyDownCallback)(Enum::SAKI key, const wchar_t[40], Keyboard* sender);
typedef void(*OnKeyReleaseCallback)(Enum::SAKI key, const wchar_t[40], Keyboard* sender);
typedef void(*OnKeyPressCallback)(Enum::SAKI key, const wchar_t[16], Keyboard* sender);
typedef void(*OnKeyDownCallback)(Enum::SAKI key, const wchar_t[16], Keyboard* sender);
typedef void(*OnKeyReleaseCallback)(Enum::SAKI key, const wchar_t[16], Keyboard* sender);
}
//-----------------------------------------------------------------------------------------------------------------------------
@ -153,7 +153,7 @@ namespace Input
public: /* Manual check functions */
virtual bool IsKeyUp (Enum::SAKI key) = 0;
virtual bool IsKeyDown (Enum::SAKI key) = 0;
virtual const wchar_t* GetAsText(Enum::SAKI key) = 0;
virtual wchar_t* GetAsText(Enum::SAKI key) = 0;
public: /* global subscribe callback functions */
void AddOnKeyPressCallback (Typedefs::OnKeyPressCallback func);
@ -164,49 +164,36 @@ namespace Input
void RemoveOnKeyDownCallback (Typedefs::OnKeyDownCallback func);
void RemoveOnKeyReleaseCallback (Typedefs::OnKeyReleaseCallback func);
public: /* From InputObject */
virtual void Activate () override { this->active = true; }
virtual void Deactivate () override { this->active = false; }
virtual bool IsActive() override { return this->active; }
public:
void operator+= (KeyboardEvent* object);
void operator-= (KeyboardEvent* object);
void BindTextTarget( ::std::wstring *field );
void ReleaseTextTarget();
public:
struct KeyboardCallbackList;
protected:
Keyboard();
~Keyboard();
protected:
struct KeyboardCallbackList
{
enum CallbackDataType
{
CallbackDataType_OnPress,
CallbackDataType_OnDown,
CallbackDataType_OnRelease
} type;
union CallbackData
{
Typedefs::OnKeyPressCallback keyPressCallback;
Typedefs::OnKeyDownCallback keyDownCallback;
Typedefs::OnKeyReleaseCallback keyReleaseCallback;
CallbackData (){ memset(this, 0, sizeof(CallbackData)); }
CallbackData (Typedefs::OnKeyPressCallback o){ keyPressCallback = o; }
bool operator ==(CallbackData o){ return o.keyDownCallback == keyDownCallback; }
bool operator ==(Typedefs::OnKeyPressCallback o ){ return o == keyPressCallback; }
operator bool(){ return this->keyDownCallback != 0; }
} function;
KeyboardCallbackList *next;
KeyboardCallbackList(CallbackData func, CallbackDataType t) :function(func), next(0), type(t) { }
};
protected:
void ClearList(KeyboardCallbackList* first);
void AddToList(Keyboard::KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data, KeyboardCallbackList::CallbackDataType type);
void RemoveFromList(KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data);
bool ExistsInList(KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data);
bool ExistsInList(std::vector<KeyboardEvent*>& list, KeyboardEvent* data);
protected: /* Internal event proc */
void InternalOnKeyPress(Enum::SAKI key, wchar_t text[16]);
void InternalOnKeyDown(Enum::SAKI key, wchar_t text[16]);
void InternalOnKeyRelease(Enum::SAKI key, wchar_t text[16]);
protected:
std::vector<KeyboardEvent*> keyEventSubscrivers;
KeyboardCallbackList* callbackList;
::std::wstring* textTarget;
::std::wstring::size_type writePos;
bool active;
};
}

View File

@ -47,7 +47,7 @@ namespace Input
typedef void(*OnMousePressCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseDownCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseReleaseCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseMoveCallback)(Struct::SAIPointInt2D, Mouse* sender);
typedef void(*OnMouseMoveCallback)(Struct::SAIPoint2D cord, Mouse* sender);
typedef void(*OnMouseScrollCallback)(int delta, Mouse* sender);
}
//-----------------------------------------------------------------------------------------------------------------------------
@ -55,24 +55,26 @@ namespace Input
class Mouse :public InputObject
{
public:
class MouseEvent
{
public:
virtual void OnMousePress ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseDown ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseRelease ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseMove ( Struct::SAIPointInt2D coordinate, Mouse* sender ) { }
virtual void OnMouseMove ( Struct::SAIPoint2D coordinate, Mouse* sender ) { }
virtual void OnMouseScroll ( int delta, Mouse* sender ) { }
};
public:
public: /* Manual check functions */
virtual bool IsBtnUp(Enum::SAMI key) = 0;
virtual bool IsBtnDown(Enum::SAMI key) = 0;
virtual int GetWheelDelta() = 0;
virtual Struct::SAIPointInt2D GetPixelPosition(Struct::SAIPointInt2D targetMem = Struct::SAIPointInt2D()) = 0;
virtual Struct::SAIPointFloat2D GetNormalizedPosition(Struct::SAIPointFloat2D targetMem = Struct::SAIPointFloat2D()) = 0;
public:
public: /* global subscribe callback functions */
int GetWheelDelta() const;
Struct::SAIPoint2D & GetPixelPosition( Struct::SAIPoint2D &targetMem = Struct::SAIPoint2D() ) const;
Struct::SAIPoint2D & GetDeltaPosition( Struct::SAIPoint2D &targetMem = Struct::SAIPoint2D() ) const;
void AddOnMousePressCallback( Typedefs::OnMousePressCallback func);
void AddOnMouseDownCallback( Typedefs::OnMouseDownCallback func );
void AddOnMouseReleaseCallback( Typedefs::OnMouseReleaseCallback func );
@ -85,61 +87,39 @@ namespace Input
void RemoveOnMouseMoveCallback( Typedefs::OnMouseMoveCallback func );
void RemoveOnMouseScrollCallback( Typedefs::OnMouseScrollCallback func );
public: /* From InputObject */
virtual void Activate () override { this->active = true; }
virtual void Deactivate () override { this->active = false; }
virtual bool IsActive() override { return this->active; }
public:
void operator+= (MouseEvent* object);
void operator-= (MouseEvent* object);
void SetPixelPos(int x, int y);
void ToggleCursor(bool toggler);
private:
void operator+= (MouseEvent* object);
void operator-= (MouseEvent* object);
public:
struct MouseCallbackList;
protected:
Mouse();
~Mouse();
protected:
struct MouseCallbackList
{
enum CallbackDataType
{
CallbackDataType_OnPress,
CallbackDataType_OnDown,
CallbackDataType_OnRelease,
CallbackDataType_OnMove,
CallbackDataType_OnScroll,
} type;
union CallbackData
{
Typedefs::OnMousePressCallback mousePressCallback;
Typedefs::OnMouseDownCallback mouseDownCallback;
Typedefs::OnMouseReleaseCallback mouseReleaseCallback;
Typedefs::OnMouseMoveCallback mouseMoveCallback;
Typedefs::OnMouseScrollCallback mouseScrollCallback;
void* dummy;
CallbackData (){ memset(this, 0, sizeof(CallbackData)); }
CallbackData (void* d){ dummy = d; }
bool operator ==(CallbackData o){ return o.dummy == dummy; }
bool operator ==(void* o ){ return o == dummy; }
operator bool(){ return this->dummy != 0; }
} function;
MouseCallbackList *next;
MouseCallbackList(CallbackData func, CallbackDataType t) :function(func), next(0), type(t) { }
};
protected:
void ClearList(MouseCallbackList* first);
void AddToList(MouseCallbackList* first, MouseCallbackList::CallbackData data, MouseCallbackList::CallbackDataType type);
void RemoveFromList(MouseCallbackList* first, MouseCallbackList::CallbackData data);
bool ExistsInList(MouseCallbackList* first, MouseCallbackList::CallbackData data);
bool ExistsInList(std::vector<MouseEvent*>& list, MouseEvent* data);
void InternalOnBtnPress(Enum::SAMI key);
void InternalOnBtnDown(Enum::SAMI key);
void InternalOnBtnRelease(Enum::SAMI key);
void InternalOnMove(Struct::SAIPoint2D cord);
void InternalOnScroll(int delta);
protected:
std::vector<MouseEvent*> mouseSubscribers;
MouseCallbackList* callbackList;
Struct::SAIPointInt2D pixelPos;
Struct::SAIPointFloat2D normalPos;
Struct::SAIPoint2D pixelPos, deltaPos;
bool isCurorLocked;
int wheelDelta;
bool active;
};
}

View File

@ -9,14 +9,15 @@
#include "Win32Keyboard.h"
#include "Win32Mouse.h"
#include <vector>
#define NOMINMAX
#include <Windows.h>
#include <map>
/**
* TODO:
* 1. Cordinate system
* 1.1. Pixel cordinates
* 1.2. 0 - 1 cordinate
* 1.1. Pixel cordinates DONE
* 1.2. 0 - 1 cordinate DONW
* 1.3. Origo in middle of the screen ( -1 to 1 )
*/
@ -42,46 +43,6 @@ namespace Input
class Win32Input :public InputManager
{
// private:
// SubscribeList<INPUT_CALLBACK, const InputData*>* _procInput;
//
// bool _enabled;
// bool _mouseEnabled;
// bool _KeyboardEnabled;
// bool _exclusive;
// List<RawInputDeviceInstance> _deviceList;
//
// List<InputData> _mouseInput;
// List<InputData> _keyboardInput;
//
// HHOOK _msgHook;
//
// private:
// Win32Input ();
// ~Win32Input ();
//
// bool _addDevice (const RAWINPUTDEVICE* k, const int& count);
// RAWINPUT*_TranslateRawInput (LPARAM l);
// void _proccessRawMouseData (RAWMOUSE&);
// void _proccessRawKeyboardData (RAWKEYBOARD&);
//
// static LRESULT CALLBACK WM_INPUT_TRANSLATE (int nCode, WPARAM wParam, LPARAM lParam);
//
// public:
//
// static Win32Input* Self ();
// static void Destroy ();
//
// const wchar_t* Input_GetError () const;
//
// bool Input_AddDevice (IN const HWND& targetApplication);
// bool Input_AddDevice (IN const RAWINPUTDEVICE*, IN const int&);
//
// void Input_Subscribe (IN INPUT_CALLBACK fnc);
// void Input_Unsubscribe (IN INPUT_CALLBACK fnc);
//
// void Input_Disable ();
// void Input_Enable ();
public:
Win32Input();
virtual~Win32Input();
@ -100,7 +61,7 @@ namespace Input
private:
static Win32Input* instance;
static void RawInputParser(HWND h, LPARAM l);
static LRESULT RawInputParser(HWND h, LPARAM l);
static LRESULT CALLBACK RawWindowCallback(HWND h, UINT m, WPARAM w, LPARAM l);
static void WindowActivate(bool activate);
};

View File

@ -5,6 +5,7 @@
#define INPUT_KEYBOARD_H
#include "..\Keyboard.h"
#define NOMINMAX
#include <Windows.h>
namespace Input
@ -17,17 +18,20 @@ namespace Input
bool IsKeyUp (Enum::SAKI key) override;
bool IsKeyDown (Enum::SAKI key) override;
const wchar_t* GetAsText(Enum::SAKI key) override;
wchar_t* GetAsText(Enum::SAKI key) override;
void ProccessKeyboardData (bool isUp, Enum::SAKI key, unsigned int makeCode, bool isE0);
void ProccessKeyboardData (RAWKEYBOARD keyboard);
private:
void MapKey(RAWKEYBOARD& rawKB, Enum::SAKI& out_key, bool& isE0);
struct Keys
{
bool isE0;
bool isDown;
unsigned int makecode;
};
static const int MAXKEYS = 256;
Keys keys[MAXKEYS];
};

View File

@ -17,13 +17,8 @@ namespace Input
bool IsBtnUp(Enum::SAMI key) override;
bool IsBtnDown(Enum::SAMI key) override;
int GetWheelDelta() override;
Struct::SAIPointInt2D GetPixelPosition(Struct::SAIPointInt2D targetMem = Struct::SAIPointInt2D()) override;
Struct::SAIPointFloat2D GetNormalizedPosition(Struct::SAIPointFloat2D targetMem = Struct::SAIPointFloat2D()) override;
void ProccessMouseData (bool isDown, Enum::SAMI btn, int delta, Struct::SAIPointInt2D velocity, unsigned int makeCode);
bool Create(HWND target);
void ProccessMouseData (RAWMOUSE mouse);
private:
struct Buttons
@ -33,7 +28,6 @@ namespace Input
};
static const int MAXBUTTONS = 25;
Buttons buttons[25];
RAWINPUTDEVICE device;
};
}

View File

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="L_inputClass.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Source\InputManager.cpp" />
<ClCompile Include="Source\Keyboard.cpp" />
<ClCompile Include="Source\Mouse.cpp" />
@ -35,37 +36,41 @@
<ClInclude Include="Include\Keyboard.h" />
<ClInclude Include="Include\Mouse.h" />
<ClInclude Include="Include\PreReq.h" />
<ClInclude Include="Include\Win32\Win32Input.h" />
<ClInclude Include="Include\Win32\Win32Keyboard.h" />
<ClInclude Include="Include\Win32\Win32Mouse.h" />
<ClInclude Include="L_inputClass.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WindowManager\WindowManager.vcxproj">
<Project>{35aea3c0-e0a7-4e1e-88cd-514aa5a442b1}</Project>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{7E3990D2-3D94-465C-B58D-64A74B3ECF9B}</ProjectGuid>
<RootNamespace>Input</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>

View File

@ -1,101 +1,38 @@
#include "RawInput.h"
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <cassert>
#include <atomic>
#include <vector>
#include <iostream>
#include <chrono>
#include <ctime>
#include <map>
#include <thread>
#include <chrono>
#include <iomanip>
#include <mutex>
#include <bitset>
#include <Windows.h>
#include "Include\Input.h"
#include "..\WindowManager\WindowShell.h"
HWND hWnd;
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
using namespace std;
int main(int agrc, char*args)
{
// sort through and find what code to run for the message given
switch(message)
WindowShell::CreateWin(WindowShell::WINDOW_INIT_DESC());
Input::Keyboard* app = Input::InputManager::Instance()->CreateKeyboardDevice();
app->Deactivate();
std::wstring text;
app->BindTextTarget( &text );
int oldLen = 0;
while (WindowShell::Frame())
{
// this message is read when the window is closed
case WM_DESTROY:
if(text.length() != oldLen)
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
case WM_KEYUP:
MessageBox(0, L"WM_KEYUP", L"", 0);
break;
case WM_KEYDOWN:
MessageBox(0, L"WM_KEYDOWN", L"", 0);
break;
//case WM_INPUT:
// MessageBox(0, L"WM_INPUT_MAIN", L"", 0);
//break;
wprintf(text.c_str());
}
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
void initWindow(HINSTANCE h, int i)
{
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = h;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
h, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, i);
}
void initRaw()
{
//RawInput::Self()->Input_AddDevice(hWnd);
}
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
initWindow(hInstance, nCmdShow);
initRaw();
// this struct holds Windows event messages
MSG msg;
// wait for the next message in the queue, store the result in 'msg'
while(GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
//RawInput::Self()->Input_Read();
}
RawInput::Destroy();
return msg.wParam;
system("pause");
}

View File

@ -8,11 +8,34 @@ using namespace Input::Enum;
using namespace Input::Typedefs;
using namespace Input::Struct;
void Keyboard::ClearList(Keyboard::KeyboardCallbackList* first)
struct Keyboard::KeyboardCallbackList
{
KeyboardCallbackList* w = first;
KeyboardCallbackList* removee = 0;
enum CallbackDataType
{
CallbackDataType_OnPress,
CallbackDataType_OnDown,
CallbackDataType_OnRelease
} type;
union CallbackData
{
Typedefs::OnKeyPressCallback keyPressCallback;
Typedefs::OnKeyDownCallback keyDownCallback;
Typedefs::OnKeyReleaseCallback keyReleaseCallback;
CallbackData (){ memset(this, 0, sizeof(CallbackData)); }
CallbackData (Typedefs::OnKeyPressCallback o){ keyPressCallback = o; }
bool operator ==(CallbackData o){ return o.keyDownCallback == keyDownCallback; }
bool operator ==(Typedefs::OnKeyPressCallback o ){ return o == keyPressCallback; }
operator bool(){ return this->keyDownCallback != 0; }
} function;
KeyboardCallbackList *next;
KeyboardCallbackList(CallbackData func, CallbackDataType t) :function(func), next(0), type(t) { }
};
void ClearList(Keyboard::KeyboardCallbackList* first)
{
Keyboard::KeyboardCallbackList* w = first;
Keyboard::KeyboardCallbackList* removee = 0;
while (w)
{
@ -21,27 +44,27 @@ void Keyboard::ClearList(Keyboard::KeyboardCallbackList* first)
delete removee;
}
}
void Keyboard::AddToList(Keyboard::KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data, KeyboardCallbackList::CallbackDataType type)
void AddToList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data, Keyboard::KeyboardCallbackList::CallbackDataType type)
{
KeyboardCallbackList *w = first;
KeyboardCallbackList *prev = first;
Keyboard::KeyboardCallbackList *w = first;
Keyboard::KeyboardCallbackList *prev = first;
while (w)
{ prev = w; w = w->next; }
KeyboardCallbackList::CallbackData f;
Keyboard::KeyboardCallbackList::CallbackData f;
f = data;
prev->next = new KeyboardCallbackList(f, type);
prev->next = new Keyboard::KeyboardCallbackList(f, type);
}
void Keyboard::RemoveFromList(KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data)
void RemoveFromList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data)
{
KeyboardCallbackList *w = first;
KeyboardCallbackList *prev = first;
Keyboard::KeyboardCallbackList *w = first;
Keyboard::KeyboardCallbackList *prev = first;
while (w)
{
if(data == w->function)
{
KeyboardCallbackList *removee = w;
Keyboard::KeyboardCallbackList *removee = w;
w = w->next;
prev->next = w;
delete removee;
@ -51,9 +74,9 @@ void Keyboard::RemoveFromList(KeyboardCallbackList* first, KeyboardCallbackList:
w = w->next;
}
}
bool Keyboard::ExistsInList(KeyboardCallbackList* first, KeyboardCallbackList::CallbackData data)
bool ExistsInList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data)
{
KeyboardCallbackList *w = first;
Keyboard::KeyboardCallbackList *w = first;
while (w)
{
if(data == w->function)
@ -64,7 +87,7 @@ bool Keyboard::ExistsInList(KeyboardCallbackList* first, KeyboardCallbackList::C
}
return true;
}
bool Keyboard::ExistsInList(std::vector<KeyboardEvent*>& list, KeyboardEvent* data)
bool ExistsInList(std::vector<Keyboard::KeyboardEvent*>& list, Keyboard::KeyboardEvent* data)
{
for (unsigned int i = 0; i < list.size(); i++)
{
@ -74,15 +97,76 @@ bool Keyboard::ExistsInList(std::vector<KeyboardEvent*>& list, KeyboardEvent* da
return false;
}
Keyboard::Keyboard()
: InputObject(SAIType_Keyboard)
, callbackList(0)
, active(1)
, textTarget(0)
, writePos(0)
{}
Keyboard::~Keyboard()
{
}
void Keyboard::InternalOnKeyPress(Enum::SAKI key, wchar_t text[16])
{
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
{
if(this->keyEventSubscrivers[i])
{
this->keyEventSubscrivers[i]->OnKeyPress(key, GetAsText(key), this);
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnPress)
w->function.keyPressCallback(key, GetAsText(key), this);
w = w->next;
}
}
void Keyboard::InternalOnKeyDown(Enum::SAKI key, wchar_t text[16])
{
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
{
if(this->keyEventSubscrivers[i])
{
this->keyEventSubscrivers[i]->OnKeyDown(key, GetAsText(key), this);
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnDown)
w->function.keyDownCallback(key, GetAsText(key), this);
w = w->next;
}
}
void Keyboard::InternalOnKeyRelease(Enum::SAKI key, wchar_t text[16])
{
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
{
if(this->keyEventSubscrivers[i])
{
this->keyEventSubscrivers[i]->OnKeyRelease(key, GetAsText(key), this);
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnRelease)
w->function.keyReleaseCallback(key, GetAsText(key), this);
w = w->next;
}
}
void Keyboard::AddOnKeyPressCallback (OnKeyPressCallback func)
{
KeyboardCallbackList::CallbackData d;
@ -134,3 +218,17 @@ void Keyboard::operator-= (KeyboardEvent* object)
}
}
void Keyboard::BindTextTarget( ::std::wstring *field )
{
this->textTarget = field;
if( field )
{
this->writePos = field->size();
}
}
void Keyboard::ReleaseTextTarget( )
{
this->BindTextTarget( nullptr );
}

View File

@ -9,11 +9,39 @@ using namespace Input::Enum;
using namespace Input::Struct;
using namespace Input::Typedefs;
void Mouse::ClearList(Mouse::MouseCallbackList* first)
struct Mouse::MouseCallbackList
{
MouseCallbackList* w = first;
MouseCallbackList* removee = 0;
enum CallbackDataType
{
CallbackDataType_OnPress,
CallbackDataType_OnDown,
CallbackDataType_OnRelease,
CallbackDataType_OnMove,
CallbackDataType_OnScroll,
} type;
union CallbackData
{
Typedefs::OnMousePressCallback mousePressCallback;
Typedefs::OnMouseDownCallback mouseDownCallback;
Typedefs::OnMouseReleaseCallback mouseReleaseCallback;
Typedefs::OnMouseMoveCallback mouseMoveCallback;
Typedefs::OnMouseScrollCallback mouseScrollCallback;
void* dummy;
CallbackData (){ memset(this, 0, sizeof(CallbackData)); }
CallbackData (void* d){ dummy = d; }
bool operator ==(CallbackData o){ return o.dummy == dummy; }
bool operator ==(void* o ){ return o == dummy; }
operator bool(){ return this->dummy != 0; }
} function;
MouseCallbackList *next;
MouseCallbackList(CallbackData func, CallbackDataType t) :function(func), next(0), type(t) { }
};
void ClearList(Mouse::MouseCallbackList* first)
{
Mouse::MouseCallbackList* w = first;
Mouse::MouseCallbackList* removee = 0;
while (w)
{
@ -22,27 +50,27 @@ void Mouse::ClearList(Mouse::MouseCallbackList* first)
delete removee;
}
}
void Mouse::AddToList(Mouse::MouseCallbackList* first, MouseCallbackList::CallbackData data, MouseCallbackList::CallbackDataType type)
void AddToList(Mouse::MouseCallbackList* first, Mouse::MouseCallbackList::CallbackData data, Mouse::MouseCallbackList::CallbackDataType type)
{
MouseCallbackList *w = first;
MouseCallbackList *prev = first;
Mouse::MouseCallbackList *w = first;
Mouse::MouseCallbackList *prev = first;
while (w)
{ prev = w; w = w->next; }
MouseCallbackList::CallbackData f;
Mouse::MouseCallbackList::CallbackData f;
f = data;
prev->next = new MouseCallbackList(f, type);
prev->next = new Mouse::MouseCallbackList(f, type);
}
void Mouse::RemoveFromList(MouseCallbackList* first, MouseCallbackList::CallbackData data)
void RemoveFromList(Mouse::MouseCallbackList* first, Mouse::MouseCallbackList::CallbackData data)
{
MouseCallbackList *w = first;
MouseCallbackList *prev = first;
Mouse::MouseCallbackList *w = first;
Mouse::MouseCallbackList *prev = first;
while (w)
{
if(data == w->function)
{
MouseCallbackList *removee = w;
Mouse::MouseCallbackList *removee = w;
w = w->next;
prev->next = w;
delete removee;
@ -52,9 +80,9 @@ void Mouse::RemoveFromList(MouseCallbackList* first, MouseCallbackList::Callback
w = w->next;
}
}
bool Mouse::ExistsInList(MouseCallbackList* first, MouseCallbackList::CallbackData data)
bool ExistsInList(Mouse::MouseCallbackList* first, Mouse::MouseCallbackList::CallbackData data)
{
MouseCallbackList *w = first;
Mouse::MouseCallbackList *w = first;
while (w)
{
if(data == w->function)
@ -65,7 +93,7 @@ bool Mouse::ExistsInList(MouseCallbackList* first, MouseCallbackList::CallbackDa
}
return true;
}
bool Mouse::ExistsInList(std::vector<MouseEvent*>& list, MouseEvent* data)
bool ExistsInList(std::vector<Mouse::MouseEvent*>& list, Mouse::MouseEvent* data)
{
for (unsigned int i = 0; i < list.size(); i++)
{
@ -81,7 +109,7 @@ Mouse::Mouse()
, wheelDelta(0)
, isCurorLocked(0)
, pixelPos()
, normalPos()
, deltaPos()
{
}
Mouse::~Mouse()
@ -89,6 +117,106 @@ Mouse::~Mouse()
}
void Mouse::InternalOnBtnPress(Enum::SAMI btn)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMousePress(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnPress)
w->function.mousePressCallback(btn, this);
w = w->next;
}
}
void Mouse::InternalOnBtnDown(Enum::SAMI btn)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseDown(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnDown)
w->function.mouseDownCallback(btn, this);
w = w->next;
}
}
void Mouse::InternalOnBtnRelease(Enum::SAMI btn)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseRelease(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnRelease)
w->function.mouseReleaseCallback(btn, this);
w = w->next;
}
}
void Mouse::InternalOnMove(Struct::SAIPoint2D cord)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseMove(this->pixelPos, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnMove)
w->function.mouseMoveCallback(this->pixelPos, this);
w = w->next;
}
}
void Mouse::InternalOnScroll(int delta)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseScroll(delta, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnMove)
w->function.mouseScrollCallback(delta, this);
w = w->next;
}
}
int Mouse::GetWheelDelta() const
{
return this->wheelDelta;
}
SAIPoint2D & Mouse::GetPixelPosition( Struct::SAIPoint2D &targetMem ) const
{
targetMem.x = this->pixelPos.x;
targetMem.y = this->pixelPos.y;
return targetMem;
}
SAIPoint2D & Mouse::GetDeltaPosition( Struct::SAIPoint2D &targetMem ) const
{
targetMem.x = this->deltaPos.x;
targetMem.y = this->deltaPos.y;
return targetMem;
}
void Mouse::AddOnMousePressCallback( Typedefs::OnMousePressCallback func)
{
MouseCallbackList::CallbackData d;
@ -150,8 +278,6 @@ void Mouse::SetPixelPos(int x, int y)
{
this->pixelPos.x = x;
this->pixelPos.y = y;
// TODO: Update normalized position
}
void Mouse::ToggleCursor(bool toggler)
{

View File

@ -16,608 +16,17 @@ using namespace Input::Enum;
using namespace Input::Struct;
using namespace Input::Typedefs;
/*
bool Win32Input::Input_AddDevice(IN const HWND& targetApplication)
{
assert(targetApplication != 0);
static const UINT c = 2;
RAWINPUTDEVICE devices[c] =
{
{ 0x01, RawInput_Usage_keyboard, RIDEV_NOLEGACY, targetApplication },
{ 0x01, RawInput_Usage_mouse, RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE, targetApplication }
};
if(! _addDevice( devices , c ) ) return false;
ShowCursor(FALSE);
//RECT r;
//GetWindow
//GetWindowRect(
return true;
}
bool Win32Input::Input_AddDevice(IN const RAWINPUTDEVICE* d, const int& count)
{
for (int i = 0; i < count; i++)
if(!d[i].hwndTarget)
{
this->_errorMsg = L"Must specify target application";
return false;
}
if(! _addDevice( d, count ) ) return false;
return true;
}
//Win32InputDEVICE d = { 0x01, type, RIDEV_REMOVE, NULL };
//this->_errorMsg = L"Failed to unregister device";
void Win32Input::Input_Disable()
{
this->_enabled = false;
}
void Win32Input::Input_Enable()
{
this->_enabled = true;
}
void Win32Input::Input_Read()
{
//for (int i = 0; i < this->_idleKeyData.size(); i++)
// this->_proccessRawKeyboardData(this->_idleKeyData.pop());
//for (int i = 0; i < this->_idleMouseData.size(); i++)
// this->_proccessRawMouseData(this->_idleMouseData.pop());
//
//this->_idleKeyData.clear();
//this->_idleMouseData.clear();
}
bool Win32Input::_addDevice (const RAWINPUTDEVICE* k, const int& count)
{
if(RegisterRawInputDevices(k, count, sizeof(RAWINPUTDEVICE)) == FALSE)
{
DWORD h = GetLastError();
INPUT_EXCEPT( L"Failed to register device" );
return false;
}
for (int q = 0; q < count; q++)
{
RawInputDeviceInstance i;
memcpy(&i.description, &k[q], sizeof(RAWINPUTDEVICE));
this->_deviceList.push(i);
}
return true;
}
*/
Win32Input *Win32Input::instance = 0;
void MapKey(RAWKEYBOARD& rawKB, bool& out_isUp, SAKI& out_key, unsigned int& sCode, bool& isE0)
{
//------------------------------------------------------------------------------------//
// http://molecularmusings.wordpress.com/2011/09/05/properly-handling-keyboard-input/ //
//------------------------------------------------------------------------------------//
UINT virtualKey = rawKB.VKey;
UINT scanCode = rawKB.MakeCode;
UINT flags = rawKB.Flags;
if (virtualKey == 255)
{
// discard "fake keys" which are part of an escaped sequence
return;
}
else if (virtualKey == VK_SHIFT)
{
// correct left-hand / right-hand SHIFT
virtualKey = MapVirtualKey(scanCode, MAPVK_VSC_TO_VK_EX);
}
else if (virtualKey == VK_NUMLOCK)
{
// correct PAUSE/BREAK and NUM LOCK silliness, and set the extended bit
scanCode = (MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC) | 0x100);
}
// e0 and e1 are escape sequences used for certain special keys, such as PRINT and PAUSE/BREAK.
// see http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
isE0 = ((flags & RI_KEY_E0) != 0);
const bool isE1 = ((flags & RI_KEY_E1) != 0);
if (isE1)
{
// for escaped sequences, turn the virtual key into the correct scan code using MapVirtualKey.
// however, MapVirtualKey is unable to map VK_PAUSE (this is a known bug), hence we map that by hand.
if (virtualKey == VK_PAUSE) scanCode = 0x45;
else scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
}
switch (virtualKey)
{
// right-hand CONTROL and ALT have their e0 bit set
case VK_CONTROL:
if (isE0) out_key = SAKI_RightCtrl;
else out_key = SAKI_LeftCtrl;
break;
case VK_MENU:
if (isE0) out_key = SAKI_RightAlt;
else out_key = SAKI_LeftAlt;
break;
// NUMPAD ENTER has its e0 bit set
case VK_RETURN:
if (isE0) out_key = SAKI_NumpadEnter;
break;
// the standard INSERT, DELETE, HOME, END, PRIOR and NEXT keys will always have their e0 bit set, but the
// corresponding keys on the NUMPAD will not.
case VK_INSERT:
if (!isE0) out_key = SAKI_Numpad0;
break;
case VK_DELETE:
if (!isE0) out_key = SAKI_NumpadDecimal;
break;
case VK_HOME:
if (!isE0) out_key = SAKI_Numpad7;
break;
case VK_END:
if (!isE0) out_key = SAKI_Numpad1;
break;
case VK_PRIOR:
if (!isE0) out_key = SAKI_Numpad9;
break;
case VK_NEXT:
if (!isE0) out_key = SAKI_Numpad3;
break;
// the standard arrow keys will always have their e0 bit set, but the
// corresponding keys on the NUMPAD will not.
case VK_LEFT:
if (!isE0) out_key = SAKI_Numpad4;
break;
case VK_RIGHT:
if (!isE0) out_key = SAKI_Numpad6;
break;
case VK_UP:
if (!isE0) out_key = SAKI_Numpad8;
break;
case VK_DOWN:
if (!isE0) out_key = SAKI_Numpad2;
break;
// NUMPAD 5 doesn't have its e0 bit set
case VK_CLEAR:
if (!isE0) out_key = SAKI_Numpad5;
break;
case 0x03 : //VK_CANCEL
break;
case 0x08 : //VK_BACK
out_key = SAKI_Backspace;
break;
case 0x09 : //VK_TAB
out_key = SAKI_Tab;
break;
case 0x10 : //VK_SHIFT
out_key = SAKI_LeftShift;
out_key = SAKI_RightShift;
break;
case 0x13 : //VK_PAUSE
out_key = SAKI_Pause;
break;
case 0x14 : //VK_CAPITAL
out_key = SAKI_CapsLock;
break;
case 0x15 : //VK_KANA
break;
case 0x1B : //VK_ESCAPE
out_key = SAKI_Escape;
break;
case 0x1C : //VK_CONVERT
break;
case 0x1D : //VK_NONCONVERT
break;
case 0x1E : //VK_ACCEPT
break;
case 0x1F : //VK_MODECHANGE
break;
case 0x20 : //VK_SPACE
out_key = SAKI_Space;
break;
case 0x29 : //VK_SELECT
break;
case 0x2A : //VK_PRINT
out_key = SAKI_PrintScreen;
break;
case 0x2B : //VK_EXECUTE
break;
case 0x2C : //VK_SNAPSHOT
break;
case 0x2F : //VK_HELP
break;
case 0x30 : //0 key
out_key = SAKI_0;
break;
case 0x31 : //1 key
out_key = SAKI_1;
break;
case 0x32 : //2 key
out_key = SAKI_2;
break;
case 0x33 : //3 key
out_key = SAKI_3;
break;
case 0x34 : //4 key
out_key = SAKI_4;
break;
case 0x35 : //5 key
out_key = SAKI_5;
break;
case 0x36 : //6 key
out_key = SAKI_6;
break;
case 0x37 : //7 key
out_key = SAKI_7;
break;
case 0x38 : //8 key
out_key = SAKI_8;
break;
case 0x39 : //9 key
out_key = SAKI_9;
break;
case 0x41 : //A key
out_key = SAKI_A;
break;
case 0x42 : //B key
out_key = SAKI_B;
break;
case 0x43 : //C key
out_key = SAKI_C;
break;
case 0x44 : //D key
out_key = SAKI_D;
break;
case 0x45 : //E key
out_key = SAKI_E;
break;
case 0x46 : //F key
out_key = SAKI_F;
break;
case 0x47 : //G key
out_key = SAKI_G;
break;
case 0x48 : //H key
out_key = SAKI_H;
break;
case 0x49 : //I key
out_key = SAKI_I;
break;
case 0x4A : //J key
out_key = SAKI_J;
break;
case 0x4B : //K key
out_key = SAKI_K;
break;
case 0x4C : //L key
out_key = SAKI_L;
break;
case 0x4D : //M key
out_key = SAKI_M;
break;
case 0x4E : //N key
out_key = SAKI_N;
break;
case 0x4F : //O key
out_key = SAKI_O;
break;
case 0x50 : //P key
out_key = SAKI_P;
break;
case 0x51 : //Q key
out_key = SAKI_Q;
break;
case 0x52 : //R key
out_key = SAKI_R;
break;
case 0x53 : //S key
out_key = SAKI_S;
break;
case 0x54 : //T key
out_key = SAKI_T;
break;
case 0x55 : //U key
out_key = SAKI_U;
break;
case 0x56 : //V key
out_key = SAKI_V;
break;
case 0x57 : //W key
out_key = SAKI_W;
break;
case 0x58 : //X key
out_key = SAKI_X;
break;
case 0x59 : //Y key
out_key = SAKI_Y;
break;
case 0x5A : //Z key
out_key = SAKI_Z;
break;
case 0x5B : //VK_LWIN
break;
case 0x5C : //VK_RWIN
break;
case 0x5D : //VK_APPS
break;
case 0x5F : //VK_SLEEP
break;
case 0x60 : //VK_NUMPAD0
out_key = SAKI_Numpad0;
break;
case 0x61 : //VK_NUMPAD1
out_key = SAKI_Numpad1;
break;
case 0x62 : //VK_NUMPAD2
out_key = SAKI_Numpad2;
break;
case 0x63 : //VK_NUMPAD3
out_key = SAKI_Numpad3;
break;
case 0x64 : //VK_NUMPAD4
out_key = SAKI_Numpad4;
break;
case 0x65 : //VK_NUMPAD5
out_key = SAKI_Numpad5;
break;
case 0x66 : //VK_NUMPAD6
out_key = SAKI_Numpad6;
break;
case 0x67 : //VK_NUMPAD7
out_key = SAKI_Numpad7;
break;
case 0x68 : //VK_NUMPAD8
out_key = SAKI_Numpad8;
break;
case 0x69 : //VK_NUMPAD9
out_key = SAKI_Numpad9;
break;
case 0x6A : //VK_MULTIPLY
out_key = SAKI_NumpadMultiply;
break;
case 0x6B : //VK_ADD
out_key = SAKI_NumpadPlus;
break;
case 0x6C : //VK_SEPARATOR
break;
case 0x6D : //VK_SUBTRACT
out_key = SAKI_NumpadSubtract;
break;
case 0x6E : //VK_DECIMAL
out_key = SAKI_NumpadDecimal;
break;
case 0x6F : //VK_DIVIDE
out_key = SAKI_NumpadDivide;
break;
case 0x70 : //VK_F1
out_key = SAKI_F1;
break;
case 0x71 : //VK_F2
out_key = SAKI_F2;
break;
case 0x72 : //VK_F3
out_key = SAKI_F3;
break;
case 0x73 : //VK_F4
out_key = SAKI_F4;
break;
case 0x74 : //VK_F5
out_key = SAKI_F5;
break;
case 0x75 : //VK_F6
out_key = SAKI_F6;
break;
case 0x76 : //VK_F7
out_key = SAKI_F7;
break;
case 0x77 : //VK_F8
out_key = SAKI_F8;
break;
case 0x78 : //VK_F9
out_key = SAKI_F9;
break;
case 0x79 : //VK_F10
out_key = SAKI_F10;
break;
case 0x7A : //VK_F11
out_key = SAKI_F11;
break;
case 0x7B : //VK_F12
out_key = SAKI_F12;
break;
case 0x7C : //VK_F13
out_key = SAKI_F13;
break;
case 0x7D : //VK_F14
out_key = SAKI_F14;
break;
case 0x7E : //VK_F15
out_key = SAKI_F15;
break;
case 0x7F : //VK_F16
out_key = SAKI_F16;
break;
case 0x80 : //VK_F17
out_key = SAKI_F17;
break;
case 0x81 : //VK_F18
out_key = SAKI_F18;
break;
case 0x82 : //VK_F19
out_key = SAKI_F19;
break;
case 0x83 : //VK_F20
out_key = SAKI_F20;
break;
case 0x84 : //VK_F21
out_key = SAKI_F21;
break;
case 0x85 : //VK_F22
out_key = SAKI_F22;
break;
case 0x86 : //VK_F23
out_key = SAKI_F23;
break;
case 0x87 : //VK_F24
out_key = SAKI_F24;
break;
case 0x90 : //VK_NUMLOCK
out_key = SAKI_Numlock;
break;
case 0x91 : //VK_SCROLL
out_key = SAKI_ScrlLock;
break;
case 0xA0 : //VK_LSHIFT
out_key = SAKI_LeftShift;
break;
case 0xA1 : //VK_RSHIFT
out_key = SAKI_RightShift;
break;
case 0xA2 : //VK_LCONTROL
out_key = SAKI_LeftCtrl;
break;
case 0xA3 : //VK_RCONTROL
out_key = SAKI_RightCtrl;
break;
case 0xA4 : //VK_LMENU
out_key = SAKI_LeftAlt;
break;
case 0xA5 : //VK_RMENU
out_key = SAKI_RightCtrl;
break;
case 0xAD : //VK_VOLUME_MUTE
out_key = SAKI_VolumeMute;
break;
case 0xAE : //VK_VOLUME_DOWN
out_key = SAKI_VolumeDown;
break;
case 0xAF : //VK_VOLUME_UP
out_key = SAKI_VolumeUp;
break;
case 0xB0 : //VK_MEDIA_NEXT_TRACK
out_key = SAKI_MediaNext;
break;
case 0xB1 : //VK_MEDIA_PREV_TRACK
out_key = SAKI_MediaPrev;
break;
case 0xB2 : //VK_MEDIA_STOP
out_key = SAKI_MediaStop;
break;
case 0xB3 : //VK_MEDIA_PLAY_PAUSE
out_key = SAKI_MediaPlayPause;
break;
case 0xBB://VK_OEM_PLUS
break;
case 0xBC://VK_OEM_COMMA
break;
case 0xBD://VK_OEM_MINUS
break;
case 0xBE://VK_OEM_PERIOD
break;
case 0xBA://VK_OEM_1
break;
case 0xBF://VK_OEM_2
break;
case 0xC0://VK_OEM_3
break;
case 0xDB://VK_OEM_4
break;
case 0xDC://VK_OEM_5
break;
case 0xDD://VK_OEM_6
break;
case 0xDE://VK_OEM_7
break;
case 0xDF://VK_OEM_8
break;
}
out_isUp = ((flags & RI_KEY_BREAK) != 0);
rawKB.MakeCode = scanCode;
sCode = scanCode;
}
void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Struct::SAIPointInt2D& vel, unsigned int& mcode)
{
if(rawMouse.lLastX != 0 || rawMouse.lLastY != 0)
{
vel.x = rawMouse.lLastX;
vel.y = rawMouse.lLastY;
}
if( rawMouse.usButtonFlags > 0 )
{
//--------------------------------------------------------------------------------------
//Mouse button pressed
if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_DOWN)
{
btn = SAMI_MouseLeftBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_DOWN)
{
btn = SAMI_MouseMiddleBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_DOWN)
{
btn = SAMI_MouseRightBtn;
isUp = false;
}
//--------------------------------------------------------------------------------------
//Mouse button Released
else if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_UP)
{
btn = SAMI_MouseLeftBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_UP)
{
btn = SAMI_MouseMiddleBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_UP)
{
btn = SAMI_MouseRightBtn;
isUp = true;
}
//--------------------------------------------------------------------------------------
else if (rawMouse.usButtonFlags == RI_MOUSE_WHEEL)
{
delta = ((int)rawMouse.usButtonData);
if(delta > 120) delta = -1;
else delta = 1;
}
}
}
void Win32Input::RawInputParser(HWND h, LPARAM l)
LRESULT Win32Input::RawInputParser(HWND h, LPARAM l)
{
LRESULT val = 0;
//Get The size of the raw data buffer
UINT bufferSize;
GetRawInputData((HRAWINPUT)l, RID_INPUT, NULL, &bufferSize, sizeof(RAWINPUTHEADER));
if (bufferSize < 1)
{
return;
}
{ return 0; }
//Create and read the raw input data
LPBYTE rawBufferIn = new BYTE[bufferSize];
@ -625,58 +34,69 @@ void Win32Input::RawInputParser(HWND h, LPARAM l)
if ( readBytes != bufferSize )
{
delete [] rawBufferIn;
return;
return 0;
}
RAWINPUT* raw = (RAWINPUT*)rawBufferIn;
if(!Win32Input::instance->enabled)
{
if(FAILED ( DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER)) ) )
{
}
val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
}
else
{
if(raw->header.dwType == RIM_TYPEMOUSE)
{
bool once = false;
for (unsigned int i = 0; i < Win32Input::instance->mouse.size(); i++)
{
bool isUp = true;
Enum::SAMI btn = Enum::SAMI_Unknown;
int delta = 0;
Struct::SAIPointInt2D vel;
unsigned int mcode = 0;
MapButton(raw->data.mouse, isUp, btn, delta, vel, mcode);
Win32Input::instance->mouse[i]->ProccessMouseData(isUp, btn, delta, vel, mcode);
if(Win32Input::instance->mouse[i]->IsActive())
{
Win32Input::instance->mouse[i]->ProccessMouseData(raw->data.mouse);
}
else
{
if(!once) val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
}
}
}
else if(raw->header.dwType == RIM_TYPEKEYBOARD)
{
bool once = false;
for (unsigned int i = 0; i < Win32Input::instance->keyboard.size(); i++)
{
bool isUp;
SAKI key = SAKI_Unknown;
unsigned int makeCode;
bool isE0;
MapKey(raw->data.keyboard, isUp, key, makeCode, isE0);
Win32Input::instance->keyboard[i]->ProccessKeyboardData(isUp, key, makeCode, isE0);
if(Win32Input::instance->keyboard[i]->IsActive())
{
Win32Input::instance->keyboard[i]->ProccessKeyboardData(raw->data.keyboard);
}
else
{
if(!once) val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
}
}
}
}
delete raw;
return val;
}
LRESULT CALLBACK Win32Input::RawWindowCallback(HWND h, UINT m, WPARAM w, LPARAM l)
{
LRESULT val = 0;
switch (m)
{
case WM_INPUT:
Win32Input::instance->RawInputParser(h, l);
return Win32Input::instance->RawInputParser(h, l);
break;
case WM_KEYDOWN:
val = 0;
break;
case WM_CHAR:
val = 0;
break;
case WM_ACTIVATE:
Win32Input::instance->WindowActivate((w == TRUE));
break;
@ -702,6 +122,7 @@ void Win32Input::WindowActivate(bool activate)
Win32Input::Win32Input()
{
this->targetHwin = 0;
if(!this->instance)
{
this->instance = this;
@ -759,21 +180,27 @@ InputObject* Win32Input::CreateDevice(const SAIType inputType, Typedefs::WindowH
case SAIType_Mouse:
{
Win32Mouse* obj = new Win32Mouse();
if(!obj->Create(this->targetHwin))
rid.usUsage = RawInput_Usage_mouse;
rid.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE;
if(RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
delete obj;
return 0;
}
int i = 0;
val = (InputObject*)1;
Win32Mouse* obj = new Win32Mouse();
this->mouse.push_back(obj);
val = obj;
}
else
{
return 0;
}
}
break;
}
return val;
}
void Win32Input::ToggleInputSystem(bool enable)
{
this->enabled = enable;
@ -797,52 +224,3 @@ void Win32Input::Destroy ()
this->keyboard.resize(0);
}
/* This method is used with hooks!
LRESULT CALLBACK RawInput::WM_INPUT_TRANSLATE (int nCode, WPARAM wParam, LPARAM lparam)
{
if (nCode < 0) return CallNextHookEx(RawInput::Self()->_msgHook, nCode, wParam, lparam);
MSG *m = (MSG*)lparam;
if(m->message == WM_INPUT)
{
RAWINPUT* raw = RawInput::Self()->_TranslateRawInput(m->lParam);
if(!raw) goto nextHook;
if(!RawInput::Self()->Self()->_enabled)
{
if(FAILED ( DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER)) ) )
RawInput::Self()->_errorMsg = L"Failed to proccess default raw input";
goto _final;
}
// if(raw->header.dwType == RIM_TYPEMOUSE) RawInput::Self()->_idleMouseData.insert(raw->data.mouse);
//else if(raw->header.dwType == RIM_TYPEKEYBOARD) RawInput::Self()->_proccessRawKeyboardData(raw->data.keyboard);
_final:
//if(FAILED ( DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER)) ) )
// RawInput::Self()->_errorMsg = L"Failed to proccess default raw input";
delete raw;
}
else if (m->message == WM_QUIT)
{
if(UnhookWindowsHookEx(RawInput::Self()->_msgHook) == FALSE)
{
RawInput::Self()->_errorMsg = L"Failed to unhook message hook!";
}
}
nextHook:
return CallNextHookEx(RawInput::Self()->_msgHook, nCode, wParam, lparam);
}
*/

View File

@ -2,10 +2,13 @@
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "..\..\Include\Win32\Win32Keyboard.h"
#include <algorithm>
#pragma warning ( disable : 4172 )
using namespace Input;
using namespace Input::Enum;
using namespace std;
Win32Keyboard::Win32Keyboard()
{
@ -24,115 +27,620 @@ bool Win32Keyboard::IsKeyDown (SAKI key)
{
return this->keys[key].isDown;
}
const wchar_t* Win32Keyboard::GetAsText(Enum::SAKI key)
wchar_t* Win32Keyboard::GetAsText(Enum::SAKI key)
{
if(Enum::SAKI_Unknown == key) return 0;
// getting a human-readable string
UINT temp = (this->keys[key].makecode << 16) | (this->keys[key].isE0 << 24);
wchar_t buff[56] = {0};
GetKeyNameTextW((LONG)temp, buff, 64);
wchar_t buff[16] = {0};
GetKeyNameTextW((LONG)temp, buff, 16);
return buff;
}
void Win32Keyboard::ProccessKeyboardData (bool isUp, SAKI key, unsigned int makeCode, bool isE0)
void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
{
if(!this->active)
{
return;
}
bool isUp = (( keyboard.Flags & RI_KEY_BREAK) != 0);
SAKI key = SAKI_Unknown;
bool isE0;
MapKey(keyboard, key, isE0);
if(key != SAKI_Unknown)
{
if(key == SAKI_Unknown) return;
//The key is released.
if(isUp)/*(k.Flags == RI_KEY_BREAK || k.Flags == (RI_KEY_BREAK | RI_KEY_E0) || k.Flags == (RI_KEY_BREAK | RI_KEY_E1))*/
{
if(key == SAKI_LeftAlt)
{ }
else if(key == SAKI_LeftCtrl)
{}
else if(key == SAKI_LeftShift)
{}
else if(key == SAKI_RightAlt)
{}
else if(key == SAKI_RightCtrl)
{}
else if(key == SAKI_RightShift)
{}
else
{
InternalOnKeyRelease(key, L"");
this->keys[key].isDown = false;
this->keys[key].isE0 = isE0;
this->keys[key].makecode = makeCode;
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
{
if(this->keyEventSubscrivers[i])
{
this->keyEventSubscrivers[i]->OnKeyRelease(key, GetAsText(key), this);
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnRelease)
w->function.keyReleaseCallback(key, GetAsText(key), this);
w = w->next;
}
}
//this->_procCollection.kd.key = (RIK)k.VKey;
//this->_procCollection.kd.released = true;
this->keys[key].makecode = keyboard.MakeCode;
}
//The key is pressed.
else /*if (k.Flags == RI_KEY_MAKE || k.Flags == (RI_KEY_MAKE | RI_KEY_E0) || k.Flags == (RI_KEY_MAKE | RI_KEY_E1))*/
{
if(key == SAKI_LeftAlt)
{}
else if(key == SAKI_LeftCtrl)
{}
else if(key == SAKI_LeftShift)
{}
else if(key == SAKI_RightAlt)
{}
else if(key == SAKI_RightCtrl)
{}
else if(key == SAKI_RightShift)
{}
else
{
if(this->keys[key].isDown)
{
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
{
if(this->keyEventSubscrivers[i])
{
this->keyEventSubscrivers[i]->OnKeyDown(key, GetAsText(key), this);
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnDown)
w->function.keyDownCallback(key, GetAsText(key), this);
w = w->next;
}
this->InternalOnKeyDown(key, L"");
}
else
{
this->InternalOnKeyPress(key, L"");
this->keys[key].isDown = true;
this->keys[key].isE0 = isE0;
this->keys[key].makecode = makeCode;
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
this->keys[key].makecode = keyboard.MakeCode;
}
}
}
if(this->textTarget)
{ //Parse text
UINT virtualKey = MapVirtualKey(keyboard.VKey, MAPVK_VK_TO_CHAR);
if(this->keys[SAKI_Backspace].isDown)
{
if(this->keyEventSubscrivers[i])
if( this->writePos > 0 )
{
this->keyEventSubscrivers[i]->OnKeyPress(key, GetAsText(key), this);
--this->writePos;
this->textTarget->erase( this->writePos, 1 );
}
}
KeyboardCallbackList *w = this->callbackList;
while (w)
else if (this->keys[SAKI_Delete].isDown)
{
if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnPress)
w->function.keyPressCallback(key, GetAsText(key), this);
w = w->next;
if( this->writePos < this->textTarget->size() )
{
this->textTarget->erase( this->writePos, 1 );
}
}
else if (this->keys[SAKI_Left].isDown)
{
this->writePos = std::max( this->writePos - 1, (wstring::size_type)0 );
}
else if (this->keys[SAKI_Right].isDown)
{
this->writePos = std::min( this->writePos + 1, this->textTarget->size() );
}
else if (virtualKey && !isUp)
{
wchar_t test = towlower((wchar_t)virtualKey);
if( this->keys[SAKI_LeftShift].isDown || this->keys[SAKI_RightShift].isDown )
test = towupper(test);
this->textTarget->insert( this->writePos, 1, test);
++this->writePos;
}
}
}
void Win32Keyboard::MapKey(RAWKEYBOARD& rawKB, SAKI& out_key, bool& isE0)
{
//------------------------------------------------------------------------------------//
// http://molecularmusings.wordpress.com/2011/09/05/properly-handling-keyboard-input/ //
//------------------------------------------------------------------------------------//
UINT virtualKey = rawKB.VKey;
UINT scanCode = rawKB.MakeCode;
UINT flags = rawKB.Flags;
if (virtualKey == 255)
{
// discard "fake keys" which are part of an escaped sequence
return;
}
else if (virtualKey == VK_SHIFT)
{
// correct left-hand / right-hand SHIFT
virtualKey = MapVirtualKey(scanCode, MAPVK_VSC_TO_VK_EX);
}
else if (virtualKey == VK_NUMLOCK)
{
// correct PAUSE/BREAK and NUM LOCK silliness, and set the extended bit
scanCode = (MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC) | 0x100);
}
// e0 and e1 are escape sequences used for certain special keys, such as PRINT and PAUSE/BREAK.
// see http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
isE0 = ((flags & RI_KEY_E0) != 0);
const bool isE1 = ((flags & RI_KEY_E1) != 0);
if (isE1)
{
// for escaped sequences, turn the virtual key into the correct scan code using MapVirtualKey.
// however, MapVirtualKey is unable to map VK_PAUSE (this is a known bug), hence we map that by hand.
if (virtualKey == VK_PAUSE) scanCode = 0x45;
else scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
}
switch (virtualKey)
{
// right-hand CONTROL and ALT have their e0 bit set
case VK_CONTROL:
if (isE0) out_key = SAKI_RightCtrl;
else out_key = SAKI_LeftCtrl;
break;
case VK_MENU:
if (isE0) out_key = SAKI_RightAlt;
else out_key = SAKI_LeftAlt;
break;
// NUMPAD ENTER has its e0 bit set
case VK_RETURN:
if (isE0) out_key = SAKI_NumpadEnter;
break;
// the standard INSERT, DELETE, HOME, END, PRIOR and NEXT keys will always have their e0 bit set, but the
// corresponding keys on the NUMPAD will not.
case VK_INSERT:
if (!isE0) out_key = SAKI_Numpad0;
break;
case VK_DELETE:
if (!isE0) out_key = SAKI_NumpadDecimal;
break;
case VK_HOME:
if (!isE0) out_key = SAKI_Numpad7;
break;
case VK_END:
if (!isE0) out_key = SAKI_Numpad1;
break;
case VK_PRIOR:
if (!isE0) out_key = SAKI_Numpad9;
break;
case VK_NEXT:
if (!isE0) out_key = SAKI_Numpad3;
break;
// the standard arrow keys will always have their e0 bit set, but the
// corresponding keys on the NUMPAD will not.
case VK_LEFT:
if (!isE0) out_key = SAKI_Numpad4;
break;
case VK_RIGHT:
if (!isE0) out_key = SAKI_Numpad6;
break;
case VK_UP:
if (!isE0) out_key = SAKI_Numpad8;
break;
case VK_DOWN:
if (!isE0) out_key = SAKI_Numpad2;
break;
// NUMPAD 5 doesn't have its e0 bit set
case VK_CLEAR:
if (!isE0) out_key = SAKI_Numpad5;
break;
case 0x03 : //VK_CANCEL
break;
case 0x08 : //VK_BACK
out_key = SAKI_Backspace;
break;
case 0x09 : //VK_TAB
out_key = SAKI_Tab;
break;
case 0x10 : //VK_SHIFT
out_key = SAKI_LeftShift;
out_key = SAKI_RightShift;
break;
case 0x13 : //VK_PAUSE
out_key = SAKI_Pause;
break;
case 0x14 : //VK_CAPITAL
out_key = SAKI_CapsLock;
break;
case 0x15 : //VK_KANA
break;
case 0x1B : //VK_ESCAPE
out_key = SAKI_Escape;
break;
case 0x1C : //VK_CONVERT
break;
case 0x1D : //VK_NONCONVERT
break;
case 0x1E : //VK_ACCEPT
break;
case 0x1F : //VK_MODECHANGE
break;
case 0x20 : //VK_SPACE
out_key = SAKI_Space;
break;
case 0x29 : //VK_SELECT
break;
case 0x2A : //VK_PRINT
out_key = SAKI_PrintScreen;
break;
case 0x2B : //VK_EXECUTE
break;
case 0x2C : //VK_SNAPSHOT
break;
case 0x2F : //VK_HELP
break;
case 0x30 : //0 key
out_key = SAKI_0;
break;
case 0x31 : //1 key
out_key = SAKI_1;
break;
case 0x32 : //2 key
out_key = SAKI_2;
break;
case 0x33 : //3 key
out_key = SAKI_3;
break;
case 0x34 : //4 key
out_key = SAKI_4;
break;
case 0x35 : //5 key
out_key = SAKI_5;
break;
case 0x36 : //6 key
out_key = SAKI_6;
break;
case 0x37 : //7 key
out_key = SAKI_7;
break;
case 0x38 : //8 key
out_key = SAKI_8;
break;
case 0x39 : //9 key
out_key = SAKI_9;
break;
case 0x41 : //A key
out_key = SAKI_A;
break;
case 0x42 : //B key
out_key = SAKI_B;
break;
case 0x43 : //C key
out_key = SAKI_C;
break;
case 0x44 : //D key
out_key = SAKI_D;
break;
case 0x45 : //E key
out_key = SAKI_E;
break;
case 0x46 : //F key
out_key = SAKI_F;
break;
case 0x47 : //G key
out_key = SAKI_G;
break;
case 0x48 : //H key
out_key = SAKI_H;
break;
case 0x49 : //I key
out_key = SAKI_I;
break;
case 0x4A : //J key
out_key = SAKI_J;
break;
case 0x4B : //K key
out_key = SAKI_K;
break;
case 0x4C : //L key
out_key = SAKI_L;
break;
case 0x4D : //M key
out_key = SAKI_M;
break;
case 0x4E : //N key
out_key = SAKI_N;
break;
case 0x4F : //O key
out_key = SAKI_O;
break;
case 0x50 : //P key
out_key = SAKI_P;
break;
case 0x51 : //Q key
out_key = SAKI_Q;
break;
case 0x52 : //R key
out_key = SAKI_R;
break;
case 0x53 : //S key
out_key = SAKI_S;
break;
case 0x54 : //T key
out_key = SAKI_T;
break;
case 0x55 : //U key
out_key = SAKI_U;
break;
case 0x56 : //V key
out_key = SAKI_V;
break;
case 0x57 : //W key
out_key = SAKI_W;
break;
case 0x58 : //X key
out_key = SAKI_X;
break;
case 0x59 : //Y key
out_key = SAKI_Y;
break;
case 0x5A : //Z key
out_key = SAKI_Z;
break;
case 0x5B : //VK_LWIN
break;
case 0x5C : //VK_RWIN
break;
case 0x5D : //VK_APPS
break;
case 0x5F : //VK_SLEEP
break;
case 0x60 : //VK_NUMPAD0
out_key = SAKI_Numpad0;
break;
case 0x61 : //VK_NUMPAD1
out_key = SAKI_Numpad1;
break;
case 0x62 : //VK_NUMPAD2
out_key = SAKI_Numpad2;
break;
case 0x63 : //VK_NUMPAD3
out_key = SAKI_Numpad3;
break;
case 0x64 : //VK_NUMPAD4
out_key = SAKI_Numpad4;
break;
case 0x65 : //VK_NUMPAD5
out_key = SAKI_Numpad5;
break;
case 0x66 : //VK_NUMPAD6
out_key = SAKI_Numpad6;
break;
case 0x67 : //VK_NUMPAD7
out_key = SAKI_Numpad7;
break;
case 0x68 : //VK_NUMPAD8
out_key = SAKI_Numpad8;
break;
case 0x69 : //VK_NUMPAD9
out_key = SAKI_Numpad9;
break;
case 0x6A : //VK_MULTIPLY
out_key = SAKI_NumpadMultiply;
break;
case 0x6B : //VK_ADD
out_key = SAKI_NumpadPlus;
break;
case 0x6C : //VK_SEPARATOR
break;
case 0x6D : //VK_SUBTRACT
out_key = SAKI_NumpadSubtract;
break;
case 0x6E : //VK_DECIMAL
out_key = SAKI_NumpadDecimal;
break;
case 0x6F : //VK_DIVIDE
out_key = SAKI_NumpadDivide;
break;
case 0x70 : //VK_F1
out_key = SAKI_F1;
break;
case 0x71 : //VK_F2
out_key = SAKI_F2;
break;
case 0x72 : //VK_F3
out_key = SAKI_F3;
break;
case 0x73 : //VK_F4
out_key = SAKI_F4;
break;
case 0x74 : //VK_F5
out_key = SAKI_F5;
break;
case 0x75 : //VK_F6
out_key = SAKI_F6;
break;
case 0x76 : //VK_F7
out_key = SAKI_F7;
break;
case 0x77 : //VK_F8
out_key = SAKI_F8;
break;
case 0x78 : //VK_F9
out_key = SAKI_F9;
break;
case 0x79 : //VK_F10
out_key = SAKI_F10;
break;
case 0x7A : //VK_F11
out_key = SAKI_F11;
break;
case 0x7B : //VK_F12
out_key = SAKI_F12;
break;
case 0x7C : //VK_F13
out_key = SAKI_F13;
break;
case 0x7D : //VK_F14
out_key = SAKI_F14;
break;
case 0x7E : //VK_F15
out_key = SAKI_F15;
break;
case 0x7F : //VK_F16
out_key = SAKI_F16;
break;
case 0x80 : //VK_F17
out_key = SAKI_F17;
break;
case 0x81 : //VK_F18
out_key = SAKI_F18;
break;
case 0x82 : //VK_F19
out_key = SAKI_F19;
break;
case 0x83 : //VK_F20
out_key = SAKI_F20;
break;
case 0x84 : //VK_F21
out_key = SAKI_F21;
break;
case 0x85 : //VK_F22
out_key = SAKI_F22;
break;
case 0x86 : //VK_F23
out_key = SAKI_F23;
break;
case 0x87 : //VK_F24
out_key = SAKI_F24;
break;
case 0x90 : //VK_NUMLOCK
out_key = SAKI_Numlock;
break;
case 0x91 : //VK_SCROLL
out_key = SAKI_ScrlLock;
break;
case 0xA0 : //VK_LSHIFT
out_key = SAKI_LeftShift;
break;
case 0xA1 : //VK_RSHIFT
out_key = SAKI_RightShift;
break;
case 0xA2 : //VK_LCONTROL
out_key = SAKI_LeftCtrl;
break;
case 0xA3 : //VK_RCONTROL
out_key = SAKI_RightCtrl;
break;
case 0xA4 : //VK_LMENU
out_key = SAKI_LeftAlt;
break;
case 0xA5 : //VK_RMENU
out_key = SAKI_RightCtrl;
break;
case 0xAD : //VK_VOLUME_MUTE
out_key = SAKI_VolumeMute;
break;
case 0xAE : //VK_VOLUME_DOWN
out_key = SAKI_VolumeDown;
break;
case 0xAF : //VK_VOLUME_UP
out_key = SAKI_VolumeUp;
break;
case 0xB0 : //VK_MEDIA_NEXT_TRACK
out_key = SAKI_MediaNext;
break;
case 0xB1 : //VK_MEDIA_PREV_TRACK
out_key = SAKI_MediaPrev;
break;
case 0xB2 : //VK_MEDIA_STOP
out_key = SAKI_MediaStop;
break;
case 0xB3 : //VK_MEDIA_PLAY_PAUSE
out_key = SAKI_MediaPlayPause;
break;
case 0xBB://VK_OEM_PLUS
break;
case 0xBC://VK_OEM_COMMA
break;
case 0xBD://VK_OEM_MINUS
break;
case 0xBE://VK_OEM_PERIOD
break;
case 0xBA://VK_OEM_1
break;
case 0xBF://VK_OEM_2
break;
case 0xC0://VK_OEM_3
break;
case 0xDB://VK_OEM_4
break;
case 0xDC://VK_OEM_5
break;
case 0xDD://VK_OEM_6
break;
case 0xDE://VK_OEM_7
break;
case 0xDF://VK_OEM_8
break;
}
}

View File

@ -1,17 +1,70 @@
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "..\..\Include\Win32\Win32Input.h"
#include "..\..\Include\Win32\Win32Mouse.h"
using namespace Input;
using namespace Input::Enum;
using namespace Input::Struct;
using namespace Input::Typedefs;
void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Struct::SAIPoint2D& vel, unsigned int& mcode)
{
if(rawMouse.lLastX != 0 || rawMouse.lLastY != 0)
{
vel.x = rawMouse.lLastX;
vel.y = rawMouse.lLastY;
}
if( rawMouse.usButtonFlags > 0 )
{
//--------------------------------------------------------------------------------------
//Mouse button pressed
if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_DOWN)
{
btn = SAMI_MouseLeftBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_DOWN)
{
btn = SAMI_MouseMiddleBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_DOWN)
{
btn = SAMI_MouseRightBtn;
isUp = false;
}
//--------------------------------------------------------------------------------------
//Mouse button Released
else if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_UP)
{
btn = SAMI_MouseLeftBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_UP)
{
btn = SAMI_MouseMiddleBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_UP)
{
btn = SAMI_MouseRightBtn;
isUp = true;
}
//--------------------------------------------------------------------------------------
else if (rawMouse.usButtonFlags == RI_MOUSE_WHEEL)
{
delta = ((int)rawMouse.usButtonData);
if(delta > 120) delta = -1;
else delta = 1;
}
}
}
Win32Mouse::Win32Mouse()
{
memset(&this->device, 0, sizeof(RAWINPUTDEVICE));
memset(&this->buttons[0], 0, sizeof(Buttons) * MAXBUTTONS);
}
Win32Mouse::~Win32Mouse()
@ -31,69 +84,26 @@ bool Win32Mouse::IsBtnDown(Enum::SAMI btn)
return this->buttons[btn].isDown;
}
int Win32Mouse::GetWheelDelta()
void Win32Mouse::ProccessMouseData (RAWMOUSE mouse)
{
return this->wheelDelta;
}
Struct::SAIPointInt2D Win32Mouse::GetPixelPosition(Struct::SAIPointInt2D targetMem)
{
targetMem = this->pixelPos;
return targetMem;
}
Struct::SAIPointFloat2D Win32Mouse::GetNormalizedPosition(Struct::SAIPointFloat2D targetMem)
{
POINT mousePos;
GetCursorPos( &mousePos );
bool isUp = true;
Enum::SAMI btn = Enum::SAMI_Unknown;
int delta = 0;
Struct::SAIPoint2D velocity;
unsigned int makeCode = 0;
MapButton(mouse, isUp, btn, delta, velocity, makeCode);
RECT windowVertex;
GetWindowRect( this->device.hwndTarget, &windowVertex );
this->normalPos.x = (float)(mousePos.x - windowVertex.left);
this->normalPos.x /= (float)(windowVertex.right - windowVertex.left);
this->normalPos.y = (float)(mousePos.y - windowVertex.top);
this->normalPos.y /= (float)(windowVertex.bottom - windowVertex.top);
return targetMem;
}
void Win32Mouse::ProccessMouseData (bool isUp, Enum::SAMI btn, int delta, Struct::SAIPointInt2D velocity, unsigned int makeCode)
{
if(velocity.Length() != 0)
{
this->pixelPos.x += velocity.x;
this->pixelPos.y += velocity.y;
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseMove(this->pixelPos, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnMove)
w->function.mouseMoveCallback(this->pixelPos, this);
w = w->next;
}
this->pixelPos.x += this->deltaPos.x = velocity.x;
this->pixelPos.y += this->deltaPos.y = velocity.y;
InternalOnMove(this->pixelPos);
}
if(delta != 0)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseScroll(delta, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnMove)
w->function.mouseScrollCallback(delta, this);
w = w->next;
}
InternalOnScroll(delta);
}
@ -105,80 +115,19 @@ void Win32Mouse::ProccessMouseData (bool isUp, Enum::SAMI btn, int delta, Struct
//The btn is released.
if(isUp)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseRelease(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnRelease)
w->function.mouseReleaseCallback(btn, this);
w = w->next;
}
InternalOnBtnRelease(btn);
}
//The btn is pressed.
else
{
//The btn is down since last frame
if(this->buttons[btn].isDown)
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMouseDown(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnDown)
w->function.mouseDownCallback(btn, this);
w = w->next;
}
InternalOnBtnDown(btn);
}
else
{
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{
if(this->mouseSubscribers[i])
this->mouseSubscribers[i]->OnMousePress(btn, this);
}
MouseCallbackList *w = this->callbackList;
while (w)
{
if(w->function)
if (w->type == MouseCallbackList::CallbackDataType_OnPress)
w->function.mousePressCallback(btn, this);
w = w->next;
InternalOnBtnPress(btn);
}
}
}
}
bool Win32Mouse::Create(HWND target)
{
this->device.usUsagePage = 0x01;
this->device.hwndTarget = target;
this->device.usUsage = Input::RawInput_Usage_mouse;
this->device.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE;
if(RegisterRawInputDevices(&this->device, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
return true;
}
memset(&this->device, 0, sizeof(RAWINPUTDEVICE));
return false;
}