Tightened up how we manage keyboard state.

master
Pacman Ghost 7 years ago
parent 3bfc521af6
commit ed192910b5
  1. 30
      MouseDll/actions.cpp
  2. 5
      MouseDll/actions.hpp
  3. 4
      MouseDll/appProfile.cpp
  4. 2
      MouseDll/appProfile.hpp
  5. 24
      MouseDll/core2.cpp
  6. 12
      MouseDll/event.cpp
  7. 5
      MouseDll/event.hpp
  8. 16
      MouseDll/globals.cpp
  9. 3
      MouseDll/globals.hpp
  10. 84
      MouseDll/keyboardState.cpp
  11. 62
      MouseDll/keyboardState.hpp
  12. 8
      MouseDll/mouse.vcproj
  13. 105
      MouseDll/sendInput.cpp
  14. 18
      MouseDll/sendInput.hpp

@ -8,7 +8,7 @@ using namespace std ;
Action::Action( const ApiAction* pAction )
: mActionParam( pAction->mActionParam )
, mKeyModifiers( pAction->mKeyModifiers )
, mKeyboardState( pAction->mKeyModifiers )
{
}
@ -53,8 +53,8 @@ Action::asString() const
// return the Action as a string
stringstream buf ;
buf << "<" << pActionName() ;
if ( keyModifiers() != 0 )
buf << ":" << keyModifiersString(keyModifiers()) ;
if ( keyboardState().isAnythingDown() )
buf << ":" << keyboardState() ;
if ( actionParam() != 0 )
buf << ":" << actionParam() ;
buf << ">" ;
@ -63,7 +63,7 @@ Action::asString() const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int Action::keyModifiers() const { return mKeyModifiers ; }
const KeyboardState& Action::keyboardState() const { return mKeyboardState ; }
int Action::actionParam() const { return mActionParam ; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -92,7 +92,7 @@ MouseLeftAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendMouseMoveInput( -scrollSize , 0 , keyModifiers() ) ;
pSendInput->sendMouseMoveInput( -scrollSize , 0 , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -115,7 +115,7 @@ MouseRightAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendMouseMoveInput( scrollSize , 0 , keyModifiers() ) ;
pSendInput->sendMouseMoveInput( scrollSize , 0 , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -138,7 +138,7 @@ MouseUpAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendMouseMoveInput( 0 , -scrollSize , keyModifiers() ) ;
pSendInput->sendMouseMoveInput( 0 , -scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -161,7 +161,7 @@ MouseDownAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendMouseMoveInput( 0 , scrollSize , keyModifiers() ) ;
pSendInput->sendMouseMoveInput( 0 , scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -184,7 +184,7 @@ WheelLeftAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = WHEEL_DELTA * scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendHorzWheelInput( -scrollSize , keyModifiers() ) ;
pSendInput->sendHorzWheelInput( -scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -207,7 +207,7 @@ WheelRightAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = WHEEL_DELTA * scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendHorzWheelInput( scrollSize , keyModifiers() ) ;
pSendInput->sendHorzWheelInput( scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -230,7 +230,7 @@ WheelUpAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = WHEEL_DELTA * scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendWheelInput( scrollSize , keyModifiers() ) ;
pSendInput->sendWheelInput( scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -253,7 +253,7 @@ WheelDownAction::doAction( void* pInfo , CSendInput* pSendInput ) const
int scrollSize = (int) pInfo ;
scrollSize = WHEEL_DELTA * scrollSize / 100 ; // unscale the movement (100 = 1 unit)
scrollSize = adjustForSpeed( scrollSize ) ;
pSendInput->sendWheelInput( -scrollSize , keyModifiers() ) ;
pSendInput->sendWheelInput( -scrollSize , keyboardState() ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -274,9 +274,9 @@ KeyPressAction::doAction( void* pInfo , CSendInput* pSendInput ) const
{
// send a "keypress" event
int vKey = actionParam() ;
// FIXME! this will save/restore the keyboard state twice!
pSendInput->sendKeyboardInput( vKey , true , keyModifiers() ) ;
pSendInput->sendKeyboardInput( vKey , false , keyModifiers() ) ;
KeyboardStateGuard keyboardStateGuard( *pSendInput , keyboardState() ) ;
pSendInput->sendKeyboardInput( vKey , true ) ;
pSendInput->sendKeyboardInput( vKey , false ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

@ -1,6 +1,7 @@
#ifndef ACTIONS_HPP
#define ACTIONS_HPP
#include "keyboardState.hpp"
#include "utils.hpp"
struct ApiAction ;
@ -33,7 +34,7 @@ public:
// access methods:
public:
int actionParam() const ;
int keyModifiers() const ;
const KeyboardState& keyboardState() const ;
virtual std::string asString() const ;
protected:
virtual const char* pActionName() const = 0 ;
@ -42,7 +43,7 @@ protected:
// data members:
private:
int mActionParam ;
int mKeyModifiers ;
KeyboardState mKeyboardState ;
} ;

@ -53,14 +53,14 @@ AppProfile::AppProfile(
// ---------------------------------------------------------------------
const Event*
AppProfile::findEvent( Event::eEventType eventType , int keyModifiers ) const
AppProfile::findEvent( Event::eEventType eventType , const KeyboardState& keyboardState ) const
{
// find the specified event
for ( EventPtrVector::const_iterator it=events().begin() ; it != events().end() ; ++it )
{
const Event* pEvent = *it ;
// FIXME! if no key modifier events are configured, then ignore keyModifiers?
if ( pEvent->eventType() == eventType && pEvent->keyModifiers() == keyModifiers )
if ( pEvent->eventType() == eventType && pEvent->keyboardState() == keyboardState )
return pEvent ;
}

@ -28,7 +28,7 @@ public:
bool fallbackToDefaultAppProfile() const ;
const EventPtrVector& events() const ;
public:
const Event* findEvent( Event::eEventType eventType , int keyModifiers ) const ;
const Event* findEvent( Event::eEventType eventType , const KeyboardState& keyboardState ) const ;
// miscellaneous methods:
public:

@ -133,16 +133,16 @@ doRunMainLoop( int* pExitFlag )
else
strokeType = stMouseMove ;
string strokeTypeString = enumString( gStrokeTypeStringTable , strokeType ) ;
int keyModifiers = CSendInput::getKeyboardState() ;
KeyboardState strokeKeyboardState = KeyboardState::getCurrKeyboardState() ;
// log the raw event
if ( isLoggingEnabled( "rawEvents" ) )
{
string keyModifiersString ;
if ( keyModifiers != 0 )
keyModifiersString = MAKE_STRING( " " << ::keyModifiersString(keyModifiers) << " ;" ) ;
string strokeKeyboardStateString ;
if ( strokeKeyboardState.isAnythingDown() )
strokeKeyboardStateString = MAKE_STRING( " " << strokeKeyboardState << " ;" ) ;
LOG_MSG(
strokeTypeString << ":" << keyModifiersString
strokeTypeString << ":" << strokeKeyboardStateString
<< " hDevice=" << hDevice
<< " ; state=0x" << hexString(pStroke->state)
<< " ; flags=0x" << hexString(pStroke->flags)
@ -218,9 +218,9 @@ doRunMainLoop( int* pExitFlag )
if ( detectMouseMove( pStrokeHistory , &eventType , &magnitude ) )
LOG_CMSG( "events" , strokeTypeString << ": " << eventType << "/" << magnitude )
// check if this event has been configured
pEvent = pAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pAppProfile->findEvent( eventType , strokeKeyboardState ) ;
if ( pEvent == NULL && pAppProfile != pDefaultAppProfile && pAppProfile->fallbackToDefaultAppProfile() )
pEvent = pDefaultAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pDefaultAppProfile->findEvent( eventType , strokeKeyboardState ) ;
// scale the movement (100 = 1 unit)
pActionInfo = (void*) (100 * magnitude) ;
}
@ -232,9 +232,9 @@ doRunMainLoop( int* pExitFlag )
LOG_CMSG( "events" , strokeTypeString << ": " << (dirn < 0?"down":"up") << "/" << wheelSize ) ;
// check if this event has been configured
Event::eEventType eventType = (dirn < 0) ? Event::etWheelDown : Event::etWheelUp ;
pEvent = pAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pAppProfile->findEvent( eventType , strokeKeyboardState ) ;
if ( pEvent == NULL && pAppProfile != pDefaultAppProfile && pAppProfile->fallbackToDefaultAppProfile() )
pEvent = pDefaultAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pDefaultAppProfile->findEvent( eventType , strokeKeyboardState ) ;
// scale the movement (100 = 1 unit)
pActionInfo = (void*) (100 * wheelSize / (WHEEL_DELTA/10)) ; // FIXME! scaling s.b. configurable
}
@ -246,9 +246,9 @@ doRunMainLoop( int* pExitFlag )
LOG_CMSG( "events" , strokeTypeString << ": " << (dirn < 0?"left":"right") << "/" << wheelSize ) ;
// check if this event has been configured
Event::eEventType eventType = (dirn < 0) ? Event::etWheelLeft : Event::etWheelRight ;
pEvent = pAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pAppProfile->findEvent( eventType , strokeKeyboardState ) ;
if ( pEvent == NULL && pAppProfile != pDefaultAppProfile && pAppProfile->fallbackToDefaultAppProfile() )
pEvent = pDefaultAppProfile->findEvent( eventType , keyModifiers ) ;
pEvent = pDefaultAppProfile->findEvent( eventType , strokeKeyboardState ) ;
// scale the movement (100 = 1 unit)
pActionInfo = (void*) (100 * wheelSize / (WHEEL_DELTA/10)) ; // FIXME! scaling s.b. configurable
}
@ -267,6 +267,8 @@ doRunMainLoop( int* pExitFlag )
// handle the event
if ( pEvent != NULL )
{
// NOTE: I tried managing the keyboard state here, so that we don't keep sending up/down events for Ctrl/Alt/Shift
// before/after every action, but it interferes with event detection, because the keyboard state is now wrong :-/
CSendInput sendInput ;
for ( ActionPtrVector::const_iterator it=pEvent->actions().begin() ; it != pEvent->actions().end() ; ++it )
{

@ -6,11 +6,9 @@ using namespace std ;
// --- CONSTRUCTORS ----------------------------------------------------
Event::Event( const ApiEvent* pEvent , const ApiAction* pActions , int nActions )
: mEventType( (eEventType)pEvent->mEventType )
, mKeyboardState( pEvent->mKeyModifiers )
{
// initialize the Event
mEventType = (eEventType) pEvent->mEventType ;
mKeyModifiers = pEvent->mKeyModifiers ;
// initialize the Event
for ( int i=0 ; i < nActions ; ++i )
mActions.push_back( Action::allocAction( pActions+i ) ) ;
@ -19,7 +17,7 @@ Event::Event( const ApiEvent* pEvent , const ApiAction* pActions , int nActions
// ---------------------------------------------------------------------
Event::eEventType Event::eventType() const { return mEventType ; }
int Event::keyModifiers() const { return mKeyModifiers ; }
const KeyboardState& Event::keyboardState() const { return mKeyboardState ; }
const ActionPtrVector& Event::actions() const { return mActions ; }
// ---------------------------------------------------------------------
@ -40,8 +38,8 @@ operator<<( ostream& os , const Event& evt )
{
// insert the Event
os << "{Event:" << evt.eventType() ;
if ( evt.keyModifiers() != 0 )
os << ":" << keyModifiersString(evt.keyModifiers()) ;
if ( evt.keyboardState().isAnythingDown() )
os << ":" << evt.keyboardState() ;
os << "}" ;
return os ;
}

@ -2,6 +2,7 @@
#define EVENT_HPP
#include "actions.hpp"
#include "keyboardState.hpp"
#include "utils.hpp"
struct ApiEvent ;
@ -26,7 +27,7 @@ public:
// access methods:
public:
eEventType eventType() const ;
int keyModifiers() const ;
const KeyboardState& keyboardState() const ;
const ActionPtrVector& actions() const ;
public:
void dumpEvent( std::ostream& os , const char* pPrefix="" ) const ;
@ -34,7 +35,7 @@ public:
// data members:
private:
eEventType mEventType ;
int mKeyModifiers ;
KeyboardState mKeyboardState ;
ActionPtrVector mActions ;
} ;

@ -20,22 +20,6 @@ PCALLBACKFN gpCallbackFn = NULL ;
// ---------------------------------------------------------------------
string
keyModifiersString( int keyModifiers )
{
// return the KeyModifiers as a string
static BitFlagsInfo stringTable[] =
{
{ kmCtrl , "Ctrl" } ,
{ kmAlt , "Alt" } ,
{ kmShift , "Shift" } ,
{ -1 , NULL } ,
} ;
return bitFlagsString( stringTable , keyModifiers , '+' ) ;
}
// ---------------------------------------------------------------------
ostream&
operator<<( ostream& os , const exception& xcptn )
{

@ -20,9 +20,6 @@ typedef std::vector<wchar_t> WideCharVector ;
typedef std::set<int> IntSet ;
typedef std::set<std::string> StringSet ;
enum eKeyModifiers { kmCtrl=0x0001 , kmAlt=0x0002 , kmShift=0x0004 } ;
extern std::string keyModifiersString( int keyModifiers ) ;
// ---------------------------------------------------------------------
extern HMODULE ghInterceptionDll ;

@ -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

@ -222,6 +222,10 @@
RelativePath=".\globals.cpp"
>
</File>
<File
RelativePath=".\keyboardState.cpp"
>
</File>
<File
RelativePath=".\sendInput.cpp"
>
@ -268,6 +272,10 @@
RelativePath=".\globals.hpp"
>
</File>
<File
RelativePath=".\keyboardState.hpp"
>
</File>
<File
RelativePath=".\sendInput.hpp"
>

@ -18,7 +18,31 @@ CSendInput::~CSendInput()
// ---------------------------------------------------------------------
void
CSendInput::sendKeyboardInput( WORD keyCode , bool keyDown , int keyModifiers )
CSendInput::setKeyboardState( const KeyboardState& newKeyboardState , const KeyboardState& currKeyboardState )
{
// set the new keyboard state
if ( newKeyboardState.isCtrlDown() && !currKeyboardState.isCtrlDown() )
sendKeyboardInput( VK_CONTROL , true ) ;
else if ( !newKeyboardState.isCtrlDown() && currKeyboardState.isCtrlDown() )
sendKeyboardInput( VK_CONTROL , false ) ;
// set the new keyboard state
if ( newKeyboardState.isAltDown() && !currKeyboardState.isAltDown() )
sendKeyboardInput( VK_MENU , true ) ;
else if ( !newKeyboardState.isAltDown() && currKeyboardState.isAltDown() )
sendKeyboardInput( VK_MENU , false ) ;
// set the new keyboard state
if ( newKeyboardState.isShiftDown() && !currKeyboardState.isShiftDown() )
sendKeyboardInput( VK_SHIFT , true ) ;
else if ( !newKeyboardState.isShiftDown() && currKeyboardState.isShiftDown() )
sendKeyboardInput( VK_SHIFT , false ) ;
}
// ---------------------------------------------------------------------
void
CSendInput::sendKeyboardInput( WORD keyCode , bool keyDown )
{
// send the keyboard input
INPUT inputRec ;
@ -27,76 +51,60 @@ CSendInput::sendKeyboardInput( WORD keyCode , bool keyDown , int keyModifiers )
inputRec.ki.wVk = keyCode ;
if ( ! keyDown )
inputRec.ki.dwFlags |= KEYEVENTF_KEYUP ;
doSendInput( &inputRec , keyModifiers ) ;
doSendInput( &inputRec ) ;
}
// ---------------------------------------------------------------------
void
CSendInput::sendMouseMoveInput( int dx , int dy , int keyModifiers )
CSendInput::sendMouseMoveInput( int dx , int dy , const KeyboardState& keyboardState )
{
// send the mouse movement input
KeyboardStateGuard keyboardStateGuard( *this , keyboardState ) ;
INPUT inputRec ;
memset( &inputRec , 0 , sizeof(INPUT) ) ;
inputRec.type = INPUT_MOUSE ;
inputRec.mi.dx = dx ;
inputRec.mi.dy = dy ;
inputRec.mi.dwFlags = MOUSEEVENTF_MOVE ;
doSendInput( &inputRec , keyModifiers ) ;
doSendInput( &inputRec ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void
CSendInput::sendWheelInput( int scrollSize , int keyModifiers )
CSendInput::sendWheelInput( int scrollSize , const KeyboardState& keyboardState )
{
// send the mouse-wheel input
KeyboardStateGuard keyboardStateGuard( *this , keyboardState ) ;
INPUT inputRec ;
memset( &inputRec , 0 , sizeof(INPUT) ) ;
inputRec.type = INPUT_MOUSE ;
inputRec.mi.dwFlags = MOUSEEVENTF_WHEEL ;
inputRec.mi.mouseData = scrollSize ; // FIXME! s.b. dwData for vista
doSendInput( &inputRec , keyModifiers ) ;
doSendInput( &inputRec ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void
CSendInput::sendHorzWheelInput( int scrollSize , int keyModifiers )
CSendInput::sendHorzWheelInput( int scrollSize , const KeyboardState& keyboardState )
{
// send the mouse-wheel input
KeyboardStateGuard keyboardStateGuard( *this , keyboardState ) ;
INPUT inputRec ;
memset( &inputRec , 0 , sizeof(INPUT) ) ;
inputRec.type = INPUT_MOUSE ;
inputRec.mi.dwFlags = MOUSEEVENTF_HWHEEL ;
inputRec.mi.mouseData = scrollSize ; // FIXME! s.b. dwData for vista
doSendInput( &inputRec , keyModifiers ) ;
doSendInput( &inputRec ) ;
}
// ---------------------------------------------------------------------
void
CSendInput::doSendInput( INPUT* pInputRec , int keyModifiers )
CSendInput::doSendInput( INPUT* pInputRec )
{
// set the keyboard state
bool isCtrlDown , isAltDown , isShiftDown ;
if ( keyModifiers >= 0 )
{
getKeyboardState( &isCtrlDown , &isAltDown , &isShiftDown ) ;
if ( (keyModifiers & kmCtrl) && !isCtrlDown )
sendKeyboardInput( VK_CONTROL , true , -1 ) ;
else if ( !(keyModifiers & kmCtrl) && isCtrlDown )
sendKeyboardInput( VK_CONTROL , false , -1 ) ;
if ( (keyModifiers & kmAlt) && !isAltDown )
sendKeyboardInput( VK_MENU , true , -1 ) ;
else if ( !(keyModifiers & kmAlt) && isAltDown )
sendKeyboardInput( VK_MENU , false , -1 ) ;
if ( (keyModifiers & kmShift) && !isShiftDown )
sendKeyboardInput( VK_SHIFT , true , -1 ) ;
else if ( !(keyModifiers & kmShift) && isShiftDown )
sendKeyboardInput( VK_SHIFT , false , -1 ) ;
}
// log the input
if ( isLoggingEnabled( "sendInput" ) )
{
@ -128,44 +136,5 @@ CSendInput::doSendInput( INPUT* pInputRec , int keyModifiers )
// Maybe because we're manipulating the Ctrl/Alt/Shift keys...?
UINT nEventRecs = SendInput( 1 , pInputRec , sizeof(INPUT) ) ;
assert( nEventRecs == 1 ) ;
Sleep( 5 ) ;
// restore the keyboard state
if ( keyModifiers >= 0 )
{
if ( (keyModifiers & kmShift) && !isShiftDown )
sendKeyboardInput( VK_SHIFT , false , -1 ) ;
else if ( !(keyModifiers & kmShift) && isShiftDown )
sendKeyboardInput( VK_SHIFT , true , -1 ) ;
if ( (keyModifiers & kmAlt) && !isAltDown )
sendKeyboardInput( VK_MENU , false , -1 ) ;
else if ( !(keyModifiers & kmAlt) && isAltDown )
sendKeyboardInput( VK_MENU , true , -1 ) ;
if ( (keyModifiers & kmCtrl) && !isCtrlDown )
sendKeyboardInput( VK_CONTROL , false , -1 ) ;
else if ( !(keyModifiers & kmCtrl) && isCtrlDown )
sendKeyboardInput( VK_CONTROL , true , -1 ) ;
}
}
// ---------------------------------------------------------------------
void
CSendInput::getKeyboardState( bool* pIsCtrlDown , bool* pIsAltDown , bool* pIsShiftDown )
{
// get the keyboard state
*pIsCtrlDown = GetAsyncKeyState( VK_CONTROL ) < 0 ;
*pIsAltDown = GetAsyncKeyState( VK_MENU ) < 0 ;
*pIsShiftDown = GetAsyncKeyState( VK_SHIFT ) < 0 ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int
CSendInput::getKeyboardState()
{
// get the keyboard state
bool isCtrlDown , isAltDown , isShiftDown ;
getKeyboardState( &isCtrlDown , &isAltDown , &isShiftDown ) ;
return (isCtrlDown ? kmCtrl : 0) | (isAltDown ? kmAlt : 0) | (isShiftDown ? kmShift : 0 ) ;
Sleep( 5 ) ; // FIXME! get rid of this
}

@ -3,6 +3,7 @@
#include <windows.h>
#include "keyboardState.hpp"
#include "utils.hpp"
// ---------------------------------------------------------------------
@ -18,17 +19,14 @@ public:
// input methods:
public:
void sendKeyboardInput( WORD keyCode , bool keyDown , int keyModifiers ) ;
void sendMouseMoveInput( int dx , int dy , int keyModifiers ) ;
void sendWheelInput( int scrollSize , int keyModifiers ) ;
void sendHorzWheelInput( int scrollSize , int keyModifiers ) ;
protected:
void doSendInput( INPUT* pInputRec , int keyModifiers ) ;
// miscellaneous methods:
void setKeyboardState( const KeyboardState& newKeyboardState , const KeyboardState& currKeyboardState ) ;
public:
static void getKeyboardState( bool* pIsCtrlDown , bool* pIsAltDown , bool* pIsShiftDown ) ;
static int getKeyboardState() ;
void sendKeyboardInput( WORD keyCode , bool keyDown ) ;
void sendMouseMoveInput( int dx , int dy , const KeyboardState& keyboardState ) ;
void sendWheelInput( int scrollSize , const KeyboardState& keyboardState ) ;
void sendHorzWheelInput( int scrollSize , const KeyboardState& keyboardState ) ;
protected:
void doSendInput( INPUT* pInputRec ) ;
} ;

Loading…
Cancel
Save