您可以使用 SDL2 跟踪不止一只鼠标吗?

Can you track more than one mouse with SDL2?

是否可以通过 SDL 2 处理两只鼠标

SDL_MOUSEBUTTONDOWN 事件中,无论使用何种鼠标,event.button.which 均为零。 (我有两个鼠标通过 USB 连接到我的 OpenSuse 机器上。)

如果是,你怎么做?

The SDL wiki 说:

Please note that this ONLY discusses "mice" with the notion of the desktop GUI. You (usually) have one system cursor, and the OS hides the hardware details from you. If you plug in 10 mice, all ten move that one cursor. For many applications and games this is perfect, and this API has served hundreds of SDL programs well since its birth.

看起来你可能不走运,至少使用普通的 SDL。由于您在 Linux 中(并且可能使用 Xorg),您可以查看 xinput handles multiple inputs; the source code for testing XI2 events 如何特别有用。

例如,在我的机器上 xinput 显示了一大堆设备:

$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Logitech USB-PS/2 Optical Mouse           id=11   [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=14   [slave  pointer  (2)]
⎜   ↳ TPPS/2 IBM TrackPoint                     id=16   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ Integrated Camera                         id=10   [slave  keyboard (3)]
    ↳ HID 046a:0011                             id=12   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
    ↳ ThinkPad Extra Buttons                    id=15   [slave  keyboard (3)]

然后我可以使用 test-xi2 命令获取一个设备(例如我的 USB 鼠标)的事件,如下所示:

$ xinput test-xi2 11
...
EVENT type 17 (RawMotion)
device: 11 (11)
detail: 0
valuators:
flags: 
      0: -5.25 (-3.00)
      1: 12.75 (8.00)
...

这只输出由我的鼠标引起的指针移动,但不输出由鼠标引起的指针移动。通过触控板。如果 xinput 是您的选择,source code 可能会让您走上正确的轨道来实现您自己的机制。