{"id":2013,"date":"2026-04-20T07:29:47","date_gmt":"2026-04-19T23:29:47","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2013"},"modified":"2026-03-28T04:53:32","modified_gmt":"2026-03-28T04:53:32","slug":"how-to-use-applicationsendkeys-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationsendkeys-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.SendKeys in the xlwings API way"},"content":{"rendered":"\n<p>The <code>SendKeys<\/code> member of the <code>Application<\/code> object in Excel is a powerful method for simulating keystrokes directly to the active application window, typically Excel itself. In xlwings, this functionality is exposed through the <code>api<\/code> property, allowing you to programmatically send key combinations that would normally be entered manually. This can be used to automate tasks like opening menus, triggering shortcuts, or interacting with dialog boxes, especially when other programmatic methods are limited. It&#8217;s particularly useful for legacy automation scenarios where UI interaction is required.<\/p>\n\n\n\n<p><strong>Syntax in xlwings:<\/strong><br>The syntax follows the Excel Object Model via the xlwings <code>api<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.SendKeys(Keys, Wait)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Keys<\/strong>: A string expression specifying the keystrokes to send. Use codes like <code>\"{F5}\"<\/code> for function keys, <code>\"^c\"<\/code> for Ctrl+C, or <code>\"%f\"<\/code> for Alt+f. Special keys are enclosed in braces (e.g., <code>\"{ENTER}\"<\/code>, <code>\"{TAB}\"<\/code>). To send literal characters, simply type them.<\/li>\n\n\n\n<li><strong>Wait<\/strong>: Optional Boolean. If <code>True<\/code>, Excel waits for the keys to be processed before continuing. If <code>False<\/code> or omitted, the macro continues immediately without waiting. Default is <code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Key Code Examples:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Key Combination<\/th><th>Code String<\/th><\/tr><\/thead><tbody><tr><td>Enter<\/td><td><code>\"{ENTER}\"<\/code><\/td><\/tr><tr><td>Ctrl+A<\/td><td><code>\"^a\"<\/code><\/td><\/tr><tr><td>Alt+F4<\/td><td><code>\"%{F4}\"<\/code><\/td><\/tr><tr><td>Shift+Tab<\/td><td><code>\"+{TAB}\"<\/code><\/td><\/tr><tr><td>Page Down<\/td><td><code>\"{PGDN}\"<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Examples in xlwings:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Activate the Find Dialog (Ctrl+F):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active # Get the active Excel application\napp.api.SendKeys(\"^f\") # Send Ctrl+F to open Find<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Refresh All Data Connections (Alt+F5):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.SendKeys(\"%{F5}\", Wait=True) # Alt+F5 and wait for completion<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Navigate and Select a Cell Range:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.SendKeys(\"{F5}\") # Open Go To dialog\napp.api.SendKeys(\"A1:D10{ENTER}\") # Type range and press Enter<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Close the Active Workbook with Save Prompt (Alt+F, then C):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.SendKeys(\"%fc\") # Alt+F to open File menu, then C for Close\n# Note: This may interact with save dialogs; handle with caution.<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `SendKeys` member of the `Application` object in Excel is a powerful method for simulating keyst&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2013","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2013"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"predecessor-version":[{"id":3072,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2013\/revisions\/3072"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}