Skip to content

Commit

Permalink
Keyboard and Mouse IsConnected method
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn committed Nov 18, 2016
1 parent dce4703 commit a600872
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Inc/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ namespace DirectX
// Reset the keyboard state
void __cdecl Reset();

// Feature detection
bool __cdecl IsConnected() const;

#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) && defined(WM_USER)
static void __cdecl ProcessMessage(UINT message, WPARAM wParam, LPARAM lParam);
#endif
Expand Down
5 changes: 4 additions & 1 deletion Inc/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ namespace DirectX

// Sets mouse mode (defaults to absolute)
void __cdecl SetMode(Mode mode);


// Feature detection
bool __cdecl IsConnected() const;

#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) && defined(WM_USER)
void __cdecl SetWindow(HWND window);
static void __cdecl ProcessMessage(UINT message, WPARAM wParam, LPARAM lParam);
Expand Down
37 changes: 37 additions & 0 deletions Src/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace
// }
//

#include <Windows.Devices.Input.h>

class Keyboard::Impl
{
public:
Expand Down Expand Up @@ -98,6 +100,26 @@ class Keyboard::Impl
memset( &mState, 0, sizeof(State) );
}

bool IsConnected() const
{
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Devices::Input;
using namespace ABI::Windows::Foundation;

ComPtr<IKeyboardCapabilities> caps;
HRESULT hr = RoActivateInstance(HStringReference(RuntimeClass_Windows_Devices_Input_KeyboardCapabilities).Get(), &caps);
ThrowIfFailed(hr);

INT32 value;
if (SUCCEEDED(caps->get_KeyboardPresent(&value)))
{
return value != 0;
}

return false;
}

void SetWindow(ABI::Windows::UI::Core::ICoreWindow* window)
{
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -294,6 +316,11 @@ class Keyboard::Impl
{
}

bool IsConnected() const
{
return false;
}

Keyboard* mOwner;

static Keyboard::Impl* s_keyboard;
Expand Down Expand Up @@ -361,6 +388,11 @@ class Keyboard::Impl
memset( &mState, 0, sizeof(State) );
}

bool IsConnected() const
{
return true;
}

State mState;
Keyboard* mOwner;

Expand Down Expand Up @@ -479,6 +511,11 @@ void Keyboard::Reset()
}


bool Keyboard::IsConnected() const
{
return pImpl->IsConnected();
}

Keyboard& Keyboard::Get()
{
if ( !Impl::s_keyboard || !Impl::s_keyboard->mOwner )
Expand Down
36 changes: 36 additions & 0 deletions Src/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ class Mouse::Impl
}
}

bool IsConnected() const
{
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Devices::Input;
using namespace ABI::Windows::Foundation;

ComPtr<IMouseCapabilities> caps;
HRESULT hr = RoActivateInstance(HStringReference(RuntimeClass_Windows_Devices_Input_MouseCapabilities).Get(), &caps);
ThrowIfFailed(hr);

INT32 value;
if (SUCCEEDED(caps->get_MousePresent(&value)))
{
return value != 0;
}

return false;
}

void SetWindow(ABI::Windows::UI::Core::ICoreWindow* window)
{
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -469,6 +489,11 @@ class Mouse::Impl
UNREFERENCED_PARAMETER(mode);
}

bool IsConnected() const
{
return false;
}

Mouse* mOwner;

static Mouse::Impl* s_mouse;
Expand Down Expand Up @@ -609,6 +634,11 @@ class Mouse::Impl
}
}

bool IsConnected() const
{
return GetSystemMetrics(SM_MOUSEPRESENT) != 0;
}

void SetWindow(HWND window)
{
if (mWindow == window)
Expand Down Expand Up @@ -926,6 +956,12 @@ void Mouse::SetMode(Mode mode)
}


bool Mouse::IsConnected() const
{
return pImpl->IsConnected();
}


Mouse& Mouse::Get()
{
if ( !Impl::s_mouse || !Impl::s_mouse->mOwner )
Expand Down

0 comments on commit a600872

Please sign in to comment.