Skip to main content
Glama

press_key

Destructive

Simulate keyboard presses with modifiers like cmd, ctrl, shift, and alt for macOS automation. Use key combos such as "cmd+c" or "ctrl+shift+F5" to automate tasks.

Instructions

Simulate a key press with optional modifiers. Accepts a key combo string like "cmd+c", "ctrl+shift+F5", or "Return". Modifiers: cmd, ctrl, shift, opt/alt.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keyYesKey combo string. Examples: "Return", "cmd+c", "ctrl+shift+F5", "alt+Tab".

Implementation Reference

  • The handler function `handlePressKey` executes the tool logic by parsing the input, validating the keys/modifiers, and executing an AppleScript command to simulate the key press on macOS.
    async function handlePressKey(
      args: Record<string, unknown>,
    ): Promise<CallToolResult> {
      const parsed = PressKeyInputSchema.parse(args);
    
      const parts = parsed.key.split("+");
      const baseKeyName = parts[parts.length - 1];
      const modifierNames = parts.slice(0, -1);
    
      // Look up the base key code (case-insensitive)
      const code = KEY_CODES_LOWER[baseKeyName.toLowerCase()];
      if (code === undefined) {
        return {
          isError: true,
          content: [
            {
              type: "text" as const,
              text: `Unknown key name: "${baseKeyName}". Valid key names include: ${KEY_NAME_EXAMPLES.join(", ")}`,
            },
          ],
        };
      }
    
      // Build AppleScript modifier clause
      const asModifiers: string[] = [];
      for (const mod of modifierNames) {
        const canonical = MODIFIER_ALIASES[mod.toLowerCase()];
        if (canonical === undefined) {
          return {
            isError: true,
            content: [
              {
                type: "text" as const,
                text: `Unknown modifier: "${mod}". Valid modifiers: cmd, ctrl, shift, opt (or alt).`,
              },
            ],
          };
        }
        asModifiers.push(`${canonical} down`);
      }
    
      // Build and execute AppleScript
      const script =
        asModifiers.length > 0
          ? `tell application "System Events" to key code ${code} using {${asModifiers.join(", ")}}`
          : `tell application "System Events" to key code ${code}`;
    
      await execFileAsync("osascript", ["-e", script], {
        timeout: APPLESCRIPT_TIMEOUT_MS,
      });
    
      return {
        content: [
          {
            type: "text" as const,
            text: JSON.stringify({
              success: true,
              key: parsed.key,
            }),
          },
        ],
      };
    }
  • The Zod schema `PressKeyInputSchema` defines the expected input format for the `press_key` tool.
    const PressKeyInputSchema = z.object({
      key: z
        .string()
        .min(1)
        .max(200)
        .describe(
          'Key combo string. Examples: "Return", "cmd+c", "ctrl+shift+F5", "alt+Tab".',
        ),
    });
  • Registration of the `press_key` tool within the `keyboardToolDefinitions` array.
      {
        name: "press_key",
        description:
          'Simulate a key press with optional modifiers. Accepts a key combo string like "cmd+c", "ctrl+shift+F5", or "Return". Modifiers: cmd, ctrl, shift, opt/alt.',
        inputSchema: zodToToolInputSchema(PressKeyInputSchema),
        annotations: {
          readOnlyHint: false,
          destructiveHint: true,
        },
      },
    ];
Behavior3/5

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

Annotations indicate destructiveHint=true and readOnlyHint=false, which the description does not contradict. However, the description adds value by explaining the format of key combos and listing modifiers, though it lacks details on system-wide effects, permissions needed, or error conditions beyond what annotations provide.

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?

The description is front-loaded with the core purpose, followed by concise details on input format and modifiers in two efficient sentences. Every sentence adds necessary information without waste.

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?

Given the tool's moderate complexity (simulating key presses with modifiers), no output schema, and annotations covering safety, the description is mostly complete. It explains the input format well but could benefit from mentioning potential side effects or system dependencies for a destructive operation.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents the 'key' parameter with examples. The description adds marginal value by listing modifiers (cmd, ctrl, shift, opt/alt) but does not provide additional syntax or format details beyond what the schema specifies.

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 ('Simulate a key press') and resource ('key combo string'), distinguishing it from siblings like 'type_text' (continuous typing) or 'click' (mouse actions). It explicitly mentions modifiers (cmd, ctrl, shift, opt/alt) to clarify scope.

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

Usage Guidelines4/5

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

The description provides clear context by specifying it accepts key combo strings with examples, but does not explicitly state when to use this versus alternatives like 'type_text' for text input or 'click' for mouse interactions. It implies usage for single key presses with modifiers.

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/antbotlab/mac-use-mcp'

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