Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Last active October 28, 2023 12:25
Show Gist options
  • Save Curtis-64/d3f64333cd631def4e320300d9790735 to your computer and use it in GitHub Desktop.
Save Curtis-64/d3f64333cd631def4e320300d9790735 to your computer and use it in GitHub Desktop.
Inline Calculator "Select Text Version"
inline Calculator with F8 Trigger
Version: 1.0
Contributors: Curtis White, GPT-4
Description
This utility listens for the F3 key press to evaluate and replace highlighted mathematical expressions inline. It handles both decimal and hexadecimal formats.
Features
One-touch inline calculation using the F8 key.
Supports decimal and hexadecimal expressions.
Robust error handling.
Requirements
Python 3.x
Libraries: pynput, pyautogui, clipboard
Setup
Install the required Python packages:
bash
Copy code
pip install pynput pyautogui clipboard
Usage
Highlight the mathematical expression you wish to evaluate.
Press F8.
The result is automatically typed next to the highlighted expression.
Code Details
Utilizes the pynput library to listen for the F8 key press.
Copies the highlighted text to the clipboard.
Evaluates the expression and outputs the result using pyautogui.
Credits
Curtis White: Initial concept and codebase.
GPT-4: Refinement and documentation.
Feel free to contribute or report issues
-------------------
from pynput import keyboard
import pyautogui
import clipboard
import re
# Debug mode flag
debug_mode = False
def debug_print(msg):
if debug_mode:
print(f"Debug: {msg}")
# Function to evaluate the expression
def evaluate_expression(expression):
try:
if "0x" in expression:
return hex(eval(expression))
else:
return eval(expression)
except Exception as e:
return f"Error: {str(e)}"
# Function to handle key release events
def on_release(key):
if key == keyboard.Key.esc: # Stop listener if 'esc' is pressed
return False
if key == keyboard.Key.f8: # Trigger when F3 is pressed
try:
# Assume the text is already selected and copied into the clipboard
pyautogui.hotkey('ctrl', 'c')
expression = clipboard.paste().strip()
result = evaluate_expression(expression)
pyautogui.typewrite(f"{result} ", interval=0.01)
except Exception as e:
debug_print(f"Error: {e}")
pyautogui.typewrite(f"Error: {e} ", interval=0.01)
# Setting up the listener
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment