Skip to main content
Glama
0xhackerfren

Frida Game Hacking MCP

by 0xhackerfren

send_key_to_window

Send keystrokes to specific windows for game automation and input simulation in the Frida Game Hacking MCP server.

Instructions

Send a keystroke to a specific window.

Args:
    target: Window title (string) or HWND handle (integer)
    key: Key to send (e.g., "a", "enter", "space", "up", "down", "left", "right")
    use_sendinput: If True, use SendInput (requires window focus). If False, use PostMessage.

Returns:
    Success status.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
targetYes
keyYes
use_sendinputNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler function implementing the send_key_to_window tool. It finds the target window by title or HWND, maps the key to a virtual key code, and sends the keystroke using either SendInput (with foreground focus) or PostMessage methods.
    def send_key_to_window(target: Union[str, int], key: str, use_sendinput: bool = True) -> Dict[str, Any]:
        """
        Send a keystroke to a specific window.
        
        Args:
            target: Window title (string) or HWND handle (integer)
            key: Key to send (e.g., "a", "enter", "space", "up", "down", "left", "right")
            use_sendinput: If True, use SendInput (requires window focus). If False, use PostMessage.
        
        Returns:
            Success status.
        """
        if not SCREENSHOT_AVAILABLE:
            return {"error": "Window control not available. Install: pip install pywin32"}
        
        # Key code mapping
        key_codes = {
            "enter": 0x0D, "return": 0x0D,
            "space": 0x20,
            "up": 0x26, "down": 0x28, "left": 0x25, "right": 0x27,
            "escape": 0x1B, "esc": 0x1B,
            "tab": 0x09,
            "backspace": 0x08,
            "w": 0x57, "a": 0x41, "s": 0x53, "d": 0x44,
            "r": 0x52, "q": 0x51, "e": 0x45, "h": 0x48, "g": 0x47,
            "p": 0x50, "l": 0x4C,
            "1": 0x31, "2": 0x32, "3": 0x33, "4": 0x34, "5": 0x35,
        }
        
        try:
            # Find the window
            hwnd = None
            if isinstance(target, int):
                hwnd = target
            else:
                def find_window(h, _):
                    nonlocal hwnd
                    if win32gui.IsWindowVisible(h):
                        title = win32gui.GetWindowText(h)
                        if title and target.lower() in title.lower():
                            hwnd = h
                            return False
                    return True
                win32gui.EnumWindows(find_window, None)
            
            if not hwnd:
                return {"error": f"Window not found: {target}"}
            
            # Get key code
            key_lower = key.lower()
            if key_lower in key_codes:
                vk_code = key_codes[key_lower]
            elif len(key) == 1:
                vk_code = ord(key.upper())
            else:
                return {"error": f"Unknown key: {key}"}
            
            import time
            
            if use_sendinput:
                # Use SendInput - requires bringing window to foreground first
                # This works better for console windows and games
                
                # Try to bring window to foreground
                try:
                    # Attach to foreground thread to allow SetForegroundWindow
                    foreground = win32gui.GetForegroundWindow()
                    foreground_thread = ctypes.windll.user32.GetWindowThreadProcessId(foreground, None)
                    current_thread = ctypes.windll.kernel32.GetCurrentThreadId()
                    
                    ctypes.windll.user32.AttachThreadInput(current_thread, foreground_thread, True)
                    win32gui.SetForegroundWindow(hwnd)
                    ctypes.windll.user32.AttachThreadInput(current_thread, foreground_thread, False)
                except:
                    pass
                
                time.sleep(0.1)
                
                # SendInput structure
                INPUT_KEYBOARD = 1
                KEYEVENTF_KEYUP = 0x0002
                
                class KEYBDINPUT(ctypes.Structure):
                    _fields_ = [
                        ("wVk", wintypes.WORD),
                        ("wScan", wintypes.WORD),
                        ("dwFlags", wintypes.DWORD),
                        ("time", wintypes.DWORD),
                        ("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))
                    ]
                
                class INPUT(ctypes.Structure):
                    class _INPUT(ctypes.Union):
                        _fields_ = [("ki", KEYBDINPUT)]
                    _anonymous_ = ("_input",)
                    _fields_ = [
                        ("type", wintypes.DWORD),
                        ("_input", _INPUT)
                    ]
                
                # Key down
                inp = INPUT(type=INPUT_KEYBOARD)
                inp.ki.wVk = vk_code
                inp.ki.wScan = 0
                inp.ki.dwFlags = 0
                inp.ki.time = 0
                inp.ki.dwExtraInfo = None
                ctypes.windll.user32.SendInput(1, ctypes.byref(inp), ctypes.sizeof(inp))
                
                time.sleep(0.05)
                
                # Key up
                inp.ki.dwFlags = KEYEVENTF_KEYUP
                ctypes.windll.user32.SendInput(1, ctypes.byref(inp), ctypes.sizeof(inp))
            else:
                # Use PostMessage (doesn't require foreground, but may not work for all windows)
                WM_KEYDOWN = 0x0100
                WM_KEYUP = 0x0101
                WM_CHAR = 0x0102
                
                if len(key) == 1 and key.isalpha():
                    win32gui.PostMessage(hwnd, WM_KEYDOWN, vk_code, 0)
                    win32gui.PostMessage(hwnd, WM_CHAR, ord(key.lower()), 0)
                    time.sleep(0.05)
                    win32gui.PostMessage(hwnd, WM_KEYUP, vk_code, 0)
                else:
                    win32gui.PostMessage(hwnd, WM_KEYDOWN, vk_code, 0)
                    time.sleep(0.05)
                    win32gui.PostMessage(hwnd, WM_KEYUP, vk_code, 0)
            
            return {
                "success": True,
                "window": win32gui.GetWindowText(hwnd),
                "key_sent": key,
                "method": "SendInput" if use_sendinput else "PostMessage"
            }
        
        except Exception as e:
            return {"error": f"Failed to send key: {str(e)}"}
  • The send_key_to_window tool is listed in the window_interaction category within the list_capabilities tool, indicating its registration in the MCP server capabilities.
    "window_interaction": [
        "list_windows", "screenshot_window", "screenshot_screen",
        "send_key_to_window", "focus_window"
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden and does well by disclosing key behavioral traits: the two different implementation methods (SendInput vs PostMessage), the focus requirement for SendInput, and the success status return. It doesn't mention error conditions, rate limits, or security implications, but covers essential operational behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Perfectly structured with a clear purpose statement followed by organized parameter explanations and return value description. Every sentence earns its place - no redundant information, no fluff, and technical details are presented in a logical, scannable format.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a 3-parameter tool with no annotations but an output schema, the description provides excellent coverage of inputs and basic behavior. The output schema handles return value details, so the description appropriately focuses on operational context. It could mention error cases or platform limitations, but covers the essential context well.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description fully compensates by providing clear semantic explanations for all 3 parameters: target accepts both string titles and integer HWND handles, key provides concrete examples of valid values, and use_sendinput explains the functional difference between the two options. This adds significant value beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('send a keystroke') and target resource ('to a specific window'), distinguishing it from siblings like 'focus_window' or 'screenshot_window'. It uses precise technical language that accurately conveys the tool's function without ambiguity.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through parameter explanations (e.g., 'requires window focus' for use_sendinput), but doesn't explicitly state when to choose this tool over alternatives like 'focus_window' or other input-related tools. No explicit when-not-to-use guidance or named alternatives are provided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xhackerfren/frida-game-hacking-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server