parent
3bfc521af6
commit
ed192910b5
@ -0,0 +1,84 @@ |
||||
#include <windows.h> |
||||
|
||||
#include "keyboardState.hpp" |
||||
#include "sendInput.hpp" |
||||
#include "utils.hpp" |
||||
|
||||
using namespace std ; |
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
KeyboardState::KeyboardState( int flags ) |
||||
: mFlags( flags ) |
||||
{ } |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
KeyboardState |
||||
KeyboardState::getCurrKeyboardState() |
||||
{ |
||||
// get the current keyboard state
|
||||
int flags = 0 ; |
||||
if ( GetAsyncKeyState( VK_CONTROL ) < 0 ) |
||||
flags |= kmCtrl ; |
||||
if ( GetAsyncKeyState( VK_MENU ) < 0 ) |
||||
flags |= kmAlt ; |
||||
if ( GetAsyncKeyState( VK_SHIFT ) < 0 ) |
||||
flags |= kmShift ; |
||||
return KeyboardState( flags ) ; |
||||
} |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
string |
||||
KeyboardState::asString() const |
||||
{ |
||||
// return the KeyboardState as a string
|
||||
static BitFlagsInfo stringTable[] = |
||||
{ |
||||
{ KeyboardState::kmCtrl , "Ctrl" } , |
||||
{ KeyboardState::kmAlt , "Alt" } , |
||||
{ KeyboardState::kmShift , "Shift" } , |
||||
{ -1 , NULL } , |
||||
} ; |
||||
return MAKE_STRING( bitFlagsString( stringTable , mFlags , '+' ) ) ; |
||||
} |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
bool KeyboardState::isCtrlDown() const { return (mFlags & kmCtrl) != 0 ; } |
||||
bool KeyboardState::isAltDown() const { return (mFlags & kmAlt) != 0 ; } |
||||
bool KeyboardState::isShiftDown() const { return (mFlags & kmShift) != 0 ; } |
||||
bool KeyboardState::isAnythingDown() const { return (mFlags != 0) ; } |
||||
|
||||
bool KeyboardState::operator==( const KeyboardState& rhs ) const { return mFlags == rhs.mFlags ; } |
||||
bool KeyboardState::operator!=( const KeyboardState& rhs ) const { return !operator==(rhs) ; } |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
ostream& |
||||
operator<<( ostream& os , const KeyboardState& keyboardState ) |
||||
{ |
||||
// insert the KeyboardState
|
||||
os << keyboardState.asString() ; |
||||
return os ; |
||||
} |
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
KeyboardStateGuard::KeyboardStateGuard( CSendInput& rSendInput , const KeyboardState& newKeyboardState ) |
||||
: mrSendInput( rSendInput ) |
||||
, mPrevKeyboardState( KeyboardState::getCurrKeyboardState() ) |
||||
, mNewKeyboardState( newKeyboardState ) |
||||
{
|
||||
// set the keyboard state
|
||||
mrSendInput.setKeyboardState( newKeyboardState , mPrevKeyboardState ) ; |
||||
} |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
KeyboardStateGuard::~KeyboardStateGuard() |
||||
{ |
||||
// restore the previous keyboard state
|
||||
mrSendInput.setKeyboardState( mPrevKeyboardState , mNewKeyboardState ) ; |
||||
} |
@ -0,0 +1,62 @@ |
||||
#ifndef KEYBOARDSTATE_HPP |
||||
#define KEYBOARDSTATE_HPP |
||||
|
||||
#include <string> |
||||
#include <iosfwd> |
||||
|
||||
class CSendInput ; |
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
class KeyboardState |
||||
{ |
||||
|
||||
// data types:
|
||||
public: |
||||
enum eKeyModifiers { kmCtrl=0x0001 , kmAlt=0x0002 , kmShift=0x0004 } ; |
||||
|
||||
// constructors/destructor:
|
||||
public: |
||||
explicit KeyboardState( int flags ) ; |
||||
|
||||
// access methods:
|
||||
public: |
||||
bool isCtrlDown() const ; |
||||
bool isAltDown() const ; |
||||
bool isShiftDown() const ; |
||||
bool isAnythingDown() const ; |
||||
std::string asString() const ; |
||||
|
||||
// keyboard state methods:
|
||||
public: |
||||
static KeyboardState getCurrKeyboardState() ; |
||||
|
||||
// operators:
|
||||
public: |
||||
bool operator==( const KeyboardState& rhs ) const ; |
||||
bool operator!=( const KeyboardState& rhs ) const ; |
||||
|
||||
// data members:
|
||||
private: |
||||
int mFlags ; |
||||
|
||||
} ; |
||||
|
||||
// inserters:
|
||||
std::ostream& operator<<( std::ostream& , const KeyboardState& ) ; |
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
struct KeyboardStateGuard |
||||
{ |
||||
KeyboardStateGuard( CSendInput& rSendInput , const KeyboardState& newKeyboardState ) ; |
||||
~KeyboardStateGuard() ; |
||||
private: |
||||
CSendInput& mrSendInput ; |
||||
KeyboardState mPrevKeyboardState ; |
||||
KeyboardState mNewKeyboardState ; |
||||
} ; |
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
#endif // KEYBOARDSTATE_HPP
|
Loading…
Reference in new issue