Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active June 24, 2024 16:20
Show Gist options
  • 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
@therealergo
Copy link

Put together a version of this which is compatible with AHK v2, with some tweaks to preserve clipboard content:

#Requires AutoHotkey v2
#SingleInstance force

; Hacky way of hiding consoles from this application
DetectHiddenWindows(true)
Run(A_ComSpec " /k",, "Hide", &pid)
while !(hConsole := WinExist("ahk_pid" pid))
	Sleep(10)
DllCall("AttachConsole", "UInt", pid)

; Define the hotkey Win + Q
#q:: {

    ; Save the current clipboard content
    originalClipboard := ClipboardAll()

    ; Clear the clipboard and send Ctrl+C to copy the current selection
    A_Clipboard := ""
    Send("^c")
    ClipWait(5)

    ; Retrieve the copied text
    inputText := A_Clipboard
    if !inputText {
        MsgBox "No text selected or copied."
        return
    }

    ; Replace Ohm sign and uppercase omega character with "ohm"
    inputText := StrReplace(inputText, "Ω", "ohm")
    inputText := StrReplace(inputText, "Ω", "ohm")

    ; Execute the qalc command and retrieve the result
    result := RunWaitOne("qalc " . "`"" . inputText . "`"")

    ; Paste the result back into the current application
    if result {
        A_Clipboard := Trim(Trim(Trim(result), "`n"), "`r")
        Send "^v"
        Sleep(500)
    } else {
        MsgBox "Failed to get a result from qalc."
    }

    ; Restore the original clipboard content
    A_Clipboard := originalClipboard
}

; Execute command and return result as string, from AHK docs
RunWaitOne(command) {
    shell := ComObject("WScript.Shell")
    exec := shell.Exec(A_ComSpec " /C " command)
    return exec.StdOut.ReadAll()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment