Skip to content

9nesh/trackforce

Repository files navigation

macOS Trackpad Pressure Monitor

Monitor Force Touch trackpad pressure on macOS with Python — integrate pressure data directly into pandas, matplotlib, PsychoPy experiments, ML models, and more.

CI Status PyPI version

Installation

pip install macos-trackpad-pressure

Note: Install name is macos-trackpad-pressure, but import as trackpad_pressure:

from trackpad_pressure import PressureMonitor, start_gui_monitor

Quick Start

GUI Monitor (Easiest)

from trackpad_pressure import start_gui_monitor

start_gui_monitor()

A window will open - press on your trackpad with varying pressure to see real-time values (0.0 to 1.0). Press Ctrl+C in the terminal to exit.

With Callback Function

from trackpad_pressure import PressureMonitor
import signal
import sys

def on_pressure(pressure, stage):
    print(f"Pressure: {pressure:.3f}, Stage: {stage}")

monitor = PressureMonitor(callback=on_pressure)

def exit_handler(signum, frame):
    print("\nStopping monitor...")
    monitor.stop()
    sys.exit(0)

signal.signal(signal.SIGINT, exit_handler)
monitor.start()

Command Line

trackpad-pressure

Device Compatibility & Behavior

Supported Devices

  • Built-in Force Touch trackpads: MacBook Pro (2015+), MacBook Air (2018+)
  • Magic Trackpad 2: Should work (tested on some systems)

How Pressure Works

  • Normalized range: Pressure values are normalized from 0.0 (no pressure) to 1.0 (maximum pressure)
  • Event-driven: Callbacks fire when pressure changes, not at a fixed rate. Frequency depends on hardware and OS
  • Window focus: The monitor window must be in focus to receive pressure events (this is a macOS security feature)

Technical Details

This package uses PyObjC to interface with macOS's Cocoa framework, specifically NSEvent.pressure() for accessing Force Touch sensor data. The API is intentionally simple: pass a callback function, get pressure values.

For more information on Force Touch behavior, see Apple's Developer Documentation.

Python Version Support

  • Python 3.7+
  • Tested on Python 3.7, 3.8, 3.9, 3.10, 3.11, 3.12

Requirements

  • macOS 10.11+ with Force Touch trackpad
  • Python 3.7+

Use Cases

This library integrates with Python's ecosystem for:

  • HCI Research: Collect pressure data alongside other behavioral metrics
  • Data Science: Export to pandas DataFrames, visualize with matplotlib/seaborn
  • Machine Learning: Training data for gesture recognition models
  • Creative Coding: Pressure-sensitive art with Processing/p5
  • Music Production: MIDI control via pressure (using mido, python-osc)
  • Game Development: Pressure controls in Pygame or Arcade
  • Accessibility Tools: Custom input methods for assistive technology
  • Education: Teaching input device concepts in Python courses

Examples

Basic Usage

See examples/simple_callback.py for a minimal example.

Data Collection for Analysis

import time
import pandas as pd
from trackpad_pressure import PressureMonitor

data = []

def collect(pressure, stage):
    data.append({
        'timestamp': time.time(),
        'pressure': pressure,
        'stage': stage
    })

monitor = PressureMonitor(callback=collect)
monitor.start()

# Later: convert to DataFrame
df = pd.DataFrame(data)
df.to_csv('pressure_data.csv')

See examples/csv_export.py for a complete example with signal handling.

Real-time Visualization

import matplotlib.pyplot as plt
from trackpad_pressure import PressureMonitor

pressures = []

def update_plot(pressure, stage):
    pressures.append(pressure)
    if len(pressures) % 10 == 0:  # Update every 10 samples
        plt.plot(pressures)
        plt.pause(0.01)

monitor = PressureMonitor(callback=update_plot)
monitor.start()

Pressure-based Thresholds

from trackpad_pressure import PressureMonitor

def on_pressure(pressure, stage):
    if pressure < 0.3:
        print("Light touch")
    elif pressure < 0.7:
        print("Medium pressure")
    else:
        print("Heavy pressure")

monitor = PressureMonitor(callback=on_pressure)
monitor.start()

More Examples

API Reference

PressureMonitor

PressureMonitor(callback=None)

Parameters:

  • callback (callable, optional): Function called when pressure changes. Receives (pressure: float, stage: int).

Methods:

  • start(gui=True): Start monitoring. Opens GUI window by default.
  • stop(): Stop monitoring and close GUI window (if open).
  • current_pressure: Property returning current pressure value (0.0-1.0)
  • current_stage: Property returning Force Touch stage (0, 1, or 2)

start_gui_monitor

start_gui_monitor(monitor=None)

Launch the GUI pressure monitor window.

Parameters:

  • monitor (PressureMonitor, optional): Pass a PressureMonitor instance to enable callbacks alongside GUI display.

Troubleshooting

Getting 1.0 for every press?

  • Requires a Force Touch trackpad (MacBook 2015+)
  • Try pressing and holding with varying pressure, not just clicking
  • Check System Preferences → Trackpad to ensure Force Touch is enabled

No pressure values appearing?

  • Make sure the monitor window is in focus (macOS only sends events to focused windows)
  • Check terminal for error messages
  • Verify you're pressing on the trackpad, not an external mouse
  • Ensure you have a Force Touch trackpad (not all MacBooks have them)

Import errors?

  • Make sure you installed with pip install macos-trackpad-pressure
  • Import as from trackpad_pressure import ... (not macos_trackpad_pressure)

Contributing

Issues and pull requests welcome on GitHub.

Changelog

See CHANGELOG.md for version history.

License

MIT


Why This Exists

I needed trackpad pressure data for an HCI experiment studying cognitive load during gaming. The experiment setup required collecting pressure alongside eye tracking and EEG data - all using Python APIs.

Couldn't find a Python package for macOS trackpad pressure. Found Swift examples, but I didn't know Swift and had a tight deadline. Used PyObjC to quickly get something working for the experiment.

After finishing, a few other researchers asked for the code for their own projects. Figured I'd clean it up and put it on PyPI in case others need it too.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages