Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active April 19, 2023 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endolith/ad36f76f40e891a18a1f3c5ca6432280 to your computer and use it in GitHub Desktop.
Save endolith/ad36f76f40e891a18a1f3c5ca6432280 to your computer and use it in GitHub Desktop.
Qalculate keyboard shortcut with AutoHotkey

Instructions:

  1. Install Qalculate
  2. Install AutoHotkey
  3. Add qalc.exe to your PATH. I use Rapid Environment Editor for this.
  4. Run qalc.ahk

To use it:

  1. Select some text, such as 1/(2*pi*10 kohm * 390 pF)
  2. Press Win+Q
  3. The text is replaced with the output of the calculation, such as 1 / (2 * pi * (10 * kiloohm) * (390 * picofarad)) = approx. 40.80895977 kHz

To do

  • Should have separate keyboard shortcuts for including the original text or not? Like how Monster.ahk has Ctrl+Win+- for replacement, and Ctrl+Win+= for appending = and the result.
; This file must be saved with UTF-8-BOM encoding to work correctly.
; Custom icon from http://www.famfamfam.com/lab/icons/silk/preview.php
; Converted from PNG to ICO
Menu, Tray, Icon, application-x-qalculate.ico
#NoEnv ; For security
; Avoids checking empty variables to see if they are environment variables (recommended for all new scripts).
#SingleInstance force
#q::
Clipboard= ; this line must be after the F12:: or it won’t get executed and the ClipWait has no effect
Send ^c
ClipWait, 1 ; put a timeout so it doesn’t get stuck if invoked with empty clipboard
if ErrorLevel ; if timeout
return
Myclip := Clipboard
Myclip := StrReplace(Myclip, "Ω", "ohm") ; Replace ohm sign (U+2126, Ω) with ohm
Myclip := StrReplace(Myclip, "Ω", "ohm") ; Replace uppercase omega character (U+03A9, Ω) with ohm
RunWait, cmd.exe /c qalc "%Myclip%" | clip,,hide
Clipboard := RegExReplace(Clipboard, "\R", "") ; Remove newline characters from the result
Send % Myclip " = " Clipboard
Sleep, 500 ; must wait to give time to paste before clearing clipboard
Clipboard=
return ; really should have this, especially if other code follows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment