Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(gameengine): -control key presses a hotkey from a script

Olcay Seyganbcc353ce7f5c
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h b/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
index 8bdc3182c..5eb88693b 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/ControlServer.h
@@ -37,7 +37,12 @@
// attack <slot> <selector> <targetSlot> <targetSelector>
// stop <slot> <selector>
//
-// plus a handful the files have no use for: ping, status, screenshot, skirmish, quit.
+// plus a handful the files have no use for: ping, status, screenshot, skirmish, quit, and
+//
+// key <KEY_name> [ALT] [CTRL] [SHIFT]
+//
+// which presses and releases one key through the message stream, the way the keyboard does. It
+// runs where it arrives, on the render pass, because that is where a real key arrives too.
//
// Replies are one JSON object per frame. Every reply carries "ok", and a failed one carries
// "error" saying what was wrong with the command rather than dropping it.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
index 7b6004403..1ee38f160 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/ControlServer.cpp
@@ -29,10 +29,13 @@
#include "Common/ControlServer.h"
#include "Common/GameEngine.h"
#include "Common/GlobalData.h"
+#include "Common/MessageStream.h"
#include "Common/Money.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "GameClient/Display.h"
+#include "GameClient/KeyDefs.h"
+#include "GameClient/MetaEvent.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Object.h"
#include "GameLogic/ScenarioDrill.h"
@@ -586,6 +589,56 @@ static void handleCommand( const AsciiString &command )
return;
}
+ /* key <KEY_name> [ALT] [CTRL] [SHIFT]
+ One press and release, carried the way Keyboard.cpp carries a real one, so the command map, the
+ translators and whatever network message sits behind them all see a key. The keyboard itself
+ is DirectInput and reads only the foreground window, which a script cannot count on holding.
+ The mouse stays where it is: post the window a WM_MOUSEMOVE first when the key reads it. */
+ if (strncmp( command.str(), "key ", 4 ) == 0)
+ {
+ char words[ 256 ];
+ strncpy( words, command.str() + 4, sizeof( words ) - 1 );
+ words[ sizeof( words ) - 1 ] = 0;
+
+ Int key = KEY_NONE;
+ const char *keyName = strtok( words, " " );
+ for( const LookupListRec *name = KeyNames; keyName && name->name; ++name )
+ {
+ if (strcmp( name->name, keyName ) == 0)
+ key = name->value;
+ }
+ if (key == KEY_NONE)
+ {
+ replyError( "key wants a KEY_ name from CommandMap.ini, e.g. key KEY_Z ALT" );
+ return;
+ }
+
+ Int modifiers = KEY_STATE_NONE;
+ for( const char *word = strtok( NULL, " " ); word; word = strtok( NULL, " " ) )
+ {
+ if (strcmp( word, "ALT" ) == 0)
+ modifiers |= KEY_STATE_LALT;
+ else if (strcmp( word, "CTRL" ) == 0)
+ modifiers |= KEY_STATE_LCONTROL;
+ else if (strcmp( word, "SHIFT" ) == 0)
+ modifiers |= KEY_STATE_LSHIFT;
+ else
+ {
+ replyError( "a modifier is ALT, CTRL or SHIFT" );
+ return;
+ }
+ }
+
+ GameMessage *press = TheMessageStream->appendMessage( GameMessage::MSG_RAW_KEY_DOWN );
+ press->appendIntegerArgument( key );
+ press->appendIntegerArgument( KEY_STATE_DOWN | modifiers );
+ GameMessage *release = TheMessageStream->appendMessage( GameMessage::MSG_RAW_KEY_UP );
+ release->appendIntegerArgument( key );
+ release->appendIntegerArgument( KEY_STATE_UP | modifiers );
+ replyOk( "\"pressed\":true" );
+ return;
+ }
+
/* skirmish <players> <seed> <map>
The map is whatever is left on the line, spaces and all, because every shipped map has a
space in its name and quoting rules would be one more thing to get wrong at the far end. */