Ejemplo n.º 1
0
// Listing accessible input devices.
func ExampleListInputDevices() {
	devices, _ := evdev.ListInputDevices()

	for _, dev := range devices {
		fmt.Printf("%s %s %s", dev.Fn, dev.Name, dev.Phys)
	}
}
Ejemplo n.º 2
0
// Select a device from a list of accessible input devices.
func select_device() (*evdev.InputDevice, error) {
	fns, _ := evdev.ListInputDevices(device_glob)
	devices := make([]*evdev.InputDevice, 0)

	for i := range fns {
		dev, err := evdev.Open(fns[i])
		if err == nil {
			devices = append(devices, dev)
		}
	}

	lines := make([]string, 0)
	max := 0
	if len(devices) > 0 {
		for i := range devices {
			dev := devices[i]
			str := fmt.Sprintf("%-3d %-20s %-35s %s", i, dev.Fn, dev.Name, dev.Phys)
			if len(str) > max {
				max = len(str)
			}
			lines = append(lines, str)
		}
		fmt.Printf("%-3s %-20s %-35s %s\n", "ID", "Device", "Name", "Phys")
		fmt.Printf(strings.Repeat("-", max) + "\n")
		fmt.Printf(strings.Join(lines, "\n") + "\n")

		var choice int
		choice_max := len(lines) - 1

	ReadChoice:
		fmt.Printf("Select device [0-%d]: ", choice_max)
		_, err := fmt.Scan(&choice)
		if err != nil || choice > choice_max || choice < 0 {
			goto ReadChoice
		}

		return devices[choice], nil
	}

	errmsg := fmt.Sprintf("no accessible input devices matched by %s", device_glob)
	return nil, errors.New(errmsg)
}