惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
D
Docker
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
月光博客
月光博客
小众软件
小众软件
量子位
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
C
Check Point Blog

Hacker News

GitHub - SeanFDZ/macmind: Single-layer transformer in HyperTalk for the classic Macintosh Show HN: Agent-cache – Multi-tier LLM/tool/session caching for Valkey and Redis Bonsai 1-bit WebGPU - a Hugging Face Space by webml-community Moving a large-scale metrics pipeline from StatsD to OpenTelemetry / Prometheus GitHub - Nightmare-Eclipse/RedSun: The Red Sun vulnerability repository GitHub - SethPyle376/hiraeth: Local AWS emulator focused on fast integration testing, with SQS support, SQLite-backed state, and a debug-friendly web UI. GitHub - macOS26/Agent: Any AI, replaces Claude Code, Cursor, OpenClaw. Over 18 LLM providers (Claude, OpenAI, Gemini, Ollama, Zai, HF, Qwen) wired into a native Mac app that writes code, builds Xcode projects, bumps versions, manages git, automates Safari, use AppleScript, JS or Accessibility, extend Agent! w/ MCP Servers, run tasks from your iPhone via Messages. YouTube now lets you turn off Shorts I Made a Terminal Pager Burgers | マクドナルド公式 Commands — HackerNews CLI documentation ChatGPT for Excel PiCore - Raspberry Pi Port of Tiny Core Linux Live Nation illegally monopolized ticketing market, jury finds Google Broke Its Promise to Me. Now ICE Has My Data. Founding Engineer at Adaptional | Y Combinator CRISPR takes important step toward silencing Down syndrome’s extra chromosome GitHub - saffron-health/libretto: The AI toolkit for building reliable browser automations US v. Heppner (S.D.N.Y. 2026) no attorney-client privilege for AI chats [pdf] Retrofitting JIT Compilers into C Interpreters IPv6 – Google The Accursèd Alphabetical Clock Cybersecurity Looks Like Proof of Work Now Fragments: April 14 Cal.com Goes Closed Source: Why AI Security Is Forcing Our Decision | Cal.com - Scheduling Software for Online Bookings Laravel raised money and now injects ads directly into your agent When moving fast, talking is the first thing to break Too much Discussion of the XOR swap trick – Heather Cafe Introduction to Spherical Harmonics for Graphics Programmers The Grand Line
Reading input from an USB Rfid Card Reader
2026-04-18 · via Hacker News

Why Bother

I had this cheap RFID reader lying around in the house. I can't right remember when and why I had bought it. I had some idea for a software, but then I never really needed to have it on the desk, therefore here you go...

When you plug it, it works like a normal keyboards, it just stays there, but then if you swipe a card or an RFID token, it will just write on your standard input, either on a terminal or whatever application you have useful at the moment.

In order to do anything useful with that piece of equipment, I had to capture the input from a single application, making sure it would not be spamming anything else.

The Name of the Beast

It turns out, what I wanted has a name, and is the EVIOCGRAB ioctl. Sure, in C that makes sense, but for higher level software components that's not a thing. I looked for examples around, and of course, Python got you covered.

But for myself I'd rather not depend on some fluffy PythonN library flying around that I need to remember to install dependencies for.

I'd rather go off and implement som kind-of-high-level Golang code.

To find my device, I had to plug it first, and look what I had available with:

find /dev/input/by-id

The Spell

Let's say now that my device has this name:

/dev/input/by-id/usb-something_Barcode_Reader-event-kbd

There you have the code to read and print the input exclusively, without affecting the other applications:

package main

import (
	"encoding/binary"
	"fmt"
	"os"
	"syscall"
	"unsafe"
)

type inputEvent struct {
	Sec   uint64
	Usec  uint64
	Type  uint16
	Code  uint16
	Value int32
}

const (
	EVIOCGRAB = 0x40044590 // ioctl to grab exclusive access

	EV_KEY    = 0x01
	KEY_ENTER = 28
)

var keyMap = map[uint16]rune{
	2: '1', 3: '2', 4: '3', 5: '4', 6: '5',
	7: '6', 8: '7', 9: '8', 10: '9', 11: '0',
	16: 'Q', 17: 'W', 18: 'E', 19: 'R', 20: 'T',
	21: 'Y', 22: 'U', 23: 'I', 24: 'O', 25: 'P',
	30: 'A', 31: 'S', 32: 'D', 33: 'F', 34: 'G',
	35: 'H', 36: 'J', 37: 'K', 38: 'L',
	44: 'Z', 45: 'X', 46: 'C', 47: 'V', 48: 'B',
	49: 'N', 50: 'M',
}

func main() {
	devPath := "/dev/input/by-id/usb-something_Barcode_Reader-event-kbd"
	if len(os.Args) > 1 {
		devPath = os.Args[1]
	}

	f, err := os.Open(devPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "open %s: %v\n", devPath, err)
		os.Exit(1)
	}
	defer f.Close()

	// Grab exclusive access so the kernel doesn't forward events to X/Wayland.
	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), EVIOCGRAB, 1); errno != 0 {
		fmt.Fprintf(os.Stderr, "EVIOCGRAB: %v\n", errno)
		os.Exit(1)
	}
	fmt.Println("Grabbed device. Waiting for card scans (Ctrl-C to exit)���")

	var card []rune
	var ev inputEvent

	for {
		if err := binary.Read(f, binary.LittleEndian, &ev); err != nil {
			fmt.Fprintf(os.Stderr, "read: %v\n", err)
			break
		}

		// We only care about key-press events (value == 1).
		if ev.Type != EV_KEY || ev.Value != 1 {
			continue
		}

		if ev.Code == KEY_ENTER {
			// RFID readers terminate each card with Enter.
			fmt.Printf("This is the RFID value: %s\n", string(card))
			card = card[:0]
			continue
		}

		if ch, ok := keyMap[ev.Code]; ok {
			card = append(card, ch)
		}
	}

	syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), EVIOCGRAB, uintptr(unsafe.Pointer(nil)))
}

Consider joining The Post Cloud ML if you find those rambling to be interesting.


[python] [golang] [git]