2010-11-29 108 views
1

我正在編寫一個程序,需要在特定時間輪詢鍵盤按鍵,而不是通過編寫自己的事件驅動鍵盤輪詢類的努力,我想我會使用JInput內置的Keyboard類。在沒有root權限的情況下在JInput中使用鍵盤

當我以root身份運行我的程序(我在Ubuntu 10.10上運行)時它完美地工作,但它甚至沒有檢測到以普通用戶身份運行時鍵盤存在的事實。我碰到下面的錯誤輸出:

Nov 29, 2010 2:08:50 PM net.java.games.input.DefaultControllerEnvironment getControllers 
INFO: Loading: net.java.games.input.LinuxEnvironmentPlugin 
Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event11): Failed to open device /dev/input/event11 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event10): Failed to open device /dev/input/event10 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event9): Failed to open device /dev/input/event9 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event8): Failed to open device /dev/input/event8 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event7): Failed to open device /dev/input/event7 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event6): Failed to open device /dev/input/event6 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event5): Failed to open device /dev/input/event5 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event4): Failed to open device /dev/input/event4 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event3): Failed to open device /dev/input/event3 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event2): Failed to open device /dev/input/event2 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event1): Failed to open device /dev/input/event1 (13) 


Nov 29, 2010 2:08:50 PM net.java.games.input.ControllerEnvironment log 
INFO: Failed to open device (/dev/input/event0): Failed to open device /dev/input/event0 (13) 

沒有人有使用JInput或如何解決這個任何想法鍵盤,所以我不必以root身份運行的經驗嗎?謝謝!

回答

1

,而不是運行程序作爲根,你也可以做

sudo chmod a+r /dev/input/event5 

(或任何設備點到鍵盤),它是由你來決定哪個是你的目的更安全,雖然。我想,但不確定,重新啓動後權限會恢復。

我永遠無法讓JInput的鍵盤適配器在Ubuntu中工作,而無需以root身份運行該應用程序或執行上述操作。可能不是你想要的答案,但它至少會告訴你你並不孤單。

爲什麼設備不能被任何人讀取的明顯原因是爲了使非特權程序(即鍵盤記錄器)不能只聽到在任何給定時間來自鍵盤的所有輸入。我認爲各種窗口系統以特權模式運行,然後控制對用戶程序的鍵盤數據的訪問。如果JInput通過該層,則可能不需要特殊權限。

編輯

因爲它似乎爲OP工作,我會在這裏包括barti_ddu的音符。如上所述,作爲手動更改鍵盤設備權限的替代方法,您還可以設置udev規則,以便自動設置這些權限。值得注意的是,你可以用它來設置設備組(比如說「輸入」),然後相應地運行程序。

這將涉及增加這樣的事情/etc/udev/rules.d/99-evdev.rules

KERNEL=="event*", NAME="input/%k", MODE="640", GROUP="input" 

警告:未經測試。

+1

簡單的udev規則會持續/ dev/input/event *權限。我寧願創建特殊的組(比如說「輸入」),然後把類似於`KERNEL ==「event *」,NAME =「input /%k」,MODE =「640」,GROUP =「input」 etc/udev/rules.d/99-evdev.rules – 2010-11-29 20:09:03

相關問題