Function Repository Resource:

Win32HotkeyHandlerSet

Source Notebook

Set up system hotkey handlers

Contributed by: Daniel Bigham

ResourceFunction["Win32HotkeyHandlerSet"][handlers,name]

sets up a handler to respond to system hotkey presses.

Details and Options

Hotkeys are only available when the Wolfram Language session is active.
The resource function Win32HotkeyHandlerClear can be used to disable hotkeys.

Examples

Basic Examples (1) 

Handler to echo "HelloWorld" when is pressed (see your "Messages" window for output):

In[1]:=
ResourceFunction["Win32HotkeyHandlerSet"][
 	{
  		{"Control", "R"} :> Echo["Hello, world!!"]
  	},
 	"MyHotkeyHandler"
 ]

Scope (1) 

Define a handler for multiple hotkeys:

In[2]:=
ResourceFunction["Win32HotkeyHandlerSet"][
 	{
  		{"Control", "R"} :> Echo["Hello, world!"],
  		{"Shift", "Control", "Alt", "O"} :> Echo["Hello, again!"]
  	},
 	"MyHotkeyHandler"
 ]

Applications (1) 

Bring Eclipse to the front when is pressed using the resource function Win32WindowToFront:

In[3]:=
ResourceFunction["Win32HotkeyHandlerSet"][
 	{
  		{"Control", "E"} :> ResourceFunction["Win32WindowToFront"][
    StringContainsQ[" - Eclipse"]]
  	},
 	"MyHotkeyHandler"
 ]

Neat Examples (1) 

Prompt for a natural language command when is pressed:

In[4]:=
grammar =
  CloudDeploy[
   GrammarRules[
    {
     FixedOrder["directions", OptionalElement["to" | "for"], city : GrammarToken["City"]] :>
      <|"Intent" -> "DirectionsTo", "Destination" -> city|>,
     FixedOrder["order" | "get" | "deliver", OptionalElement["me"], OptionalElement["some"], "pizza"] :>
      <|"Intent" -> "OrderPizza"|>
     }
    ],
   "MyGrammar"
   ];
ResourceFunction["Win32HotkeyHandlerSet"][
 {
  {"Control", "Q"} :> (
    input = InputString[
      "I'm Wolfie, your Wolfram Language assistant.\nYou can say things like \"directions to NYC\" and \"order pizza\"."];
    If [input =!= $Canceled,
     Replace[
      GrammarApply[grammar, input],
      {
       KeyValuePattern[{"Intent" -> "DirectionsTo", "Destination" -> d_}] :> MessageDialog[
         Button["Open Webpage", SystemOpen[
           "https://www.google.com/maps/dir/?api=1&destination=" <> d["Label"]]]],
       KeyValuePattern["Intent" -> "OrderPizza"] :> MessageDialog[
         Button["Open Webpage", SystemOpen[
           "https://www.dominos.ca/pages/order/#!/locations/search/"]]],
       _ :> MessageDialog["Sorry, I didn't understand."]
       }
      ]
     ]
    )
  },
 "MyHotkeyHandler"
 ]

Publisher

Daniel Bigham

Version History

  • 1.0.0 – 23 January 2020

Related Resources

License Information