User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

[JAVA]KeyEvent - Distinguish between two keyboards

Sat Dec 31, 2016 4:58 pm

Hello everyone,

I'm from germany...so forgive my bad english.

I'm going to use a RaspPI 3 with Raspbian Jessie for a Dartscore-Software which includes
two seperated boards as windows in a frame.
Two keyboards are plugged in via USB in the same Raspberry.
I want to use one Software on one Raspberry and seperate the two keyboards to it to display
it on one screen.

For example:

We have Keyboard A and Keyboard B
We have the software with Board 1 and Board 2.

If A types in the score, only Board 1 should react.
If B types in the score, only Board 2 should react.

Is there anyway I didn't think off to solve the problem?
Is there something I missing?

I really hope someone can help me.
If another programming language could solve the problem, please
let me know.

Thanks in advance
JoGGi
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

knute
Posts: 354
Joined: Thu Oct 23, 2014 12:14 am

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Sat Dec 31, 2016 5:21 pm

Two Pis, two keyboards, network them together to decide the winner.

User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Sat Dec 31, 2016 5:29 pm

Hey knute,

this isn't possible. There is only one TV! I'm not able to setup 2 Pi's as a
splitscreen. There must be another possibility like the RawKeyboardInput in C#
for using in linux.

In the hwinfo I can sperate them but not in Java I think. Any other solutions?

Greetings
JoGGi
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

clicky
Posts: 226
Joined: Thu Oct 25, 2012 7:34 am

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Mon Jan 02, 2017 9:36 pm

OK - I am not sure you can exactly distinguish between two, but... X windows can be set up in such way that there are two (or more!) keyboards (and mice) each (pair) belonging to separate 'focus'. So practically you can attach two keyboards and two mice, do the setup and have two people working on the same computer, same screen but separate programs (or separate windows of a same program) in parallel to each other. They can even focus on the same window and compete for typing! ;)

Maybe you can set up your application to have two windows and one keyboard to (somehow?!) focus to one window and another keyboard to another windows. Hm...

But, there might be another way if you can directly read from /sys/class/input/input<number> file... Unfortunately you would need to do your own translations... I managed to dig out a link talking about that: https://www.reddit.com/r/javahelp/comme ... nux_using/

User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Tue Jan 03, 2017 6:18 pm

Hi clicky,
thank you for your help. I will try this out and write back how it's going on.

Bye
JoGGi
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

knute
Posts: 354
Joined: Thu Oct 23, 2014 12:14 am

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Wed Jan 04, 2017 5:13 pm

joggiman wrote:Hey knute,

this isn't possible. There is only one TV! I'm not able to setup 2 Pi's as a
splitscreen. There must be another possibility like the RawKeyboardInput in C#
for using in linux.

In the hwinfo I can sperate them but not in Java I think. Any other solutions?

Greetings
JoGGi
Have one Pi running the main program and connected to the TV. Have the second Pi connect to your program via network and display its input. I think you could even collect KeyEvents from the headless Pi and send them to the part of your program that displays that side.

Just for my curiosity what happens when you connect two keyboards to a Pi and listen for KeyEvents?

User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Wed Jan 04, 2017 9:09 pm

Hey guys,

I tried the code click showed me and it throws an IOException: Invalid argument. I tried many things to fix this, but I think I'm not able to :D And yes, it is the hole error message that's been thrown.

I'm that near to solve it...any ideas? I used the same code like's been posted in the above link.

Thank you all. You're all a great help to me!
JoGGi
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Wed Jan 04, 2017 9:44 pm

knute wrote:
joggiman wrote:Hey knute,

this isn't possible. There is only one TV! I'm not able to setup 2 Pi's as a
splitscreen. There must be another possibility like the RawKeyboardInput in C#
for using in linux.

In the hwinfo I can sperate them but not in Java I think. Any other solutions?

Greetings
JoGGi
Have one Pi running the main program and connected to the TV. Have the second Pi connect to your program via network and display its input. I think you could even collect KeyEvents from the headless Pi and send them to the part of your program that displays that side.

Just for my curiosity what happens when you connect two keyboards to a Pi and listen for KeyEvents?
If I use two keyboards and listen to the KeyEvent both keyboards can write and there is no seeing difference between these inputs....
Oh, now I see something wrong in my last post: I'm not allowed to use two pi's...on Windows there's the RawKeyboardInput to seperate them...in Linux is the dir to look at....I think there's a solution and I just have to figure it out how to solve my problem in Linux...If I could change the sending data from keyboard with an ID, it should be the easiest way...but is it? :D
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

ghans
Posts: 7806
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Thu Jan 05, 2017 9:00 am

http://stackoverflow.com/questions/2937 ... put-eventx

That should fix the IOException.


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

clicky
Posts: 226
Joined: Thu Oct 25, 2012 7:34 am

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Thu Jan 05, 2017 9:45 am

Oh. This is so interesting! Please do keep posting your progress here! :)

BTW is your code on github maybe? Or do you plan making it open sourced? If not the whole lot - you can make a simple library which is and that would be prefect place for people to help you - maybe even contribute towards it, so you can use it in your project. Why not!

User avatar
joggiman
Posts: 6
Joined: Sat Dec 31, 2016 4:45 pm
Location: Germany

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Sat Jan 07, 2017 5:49 pm

Hey all,

a little Update:
Big thanks to ghans! This was the solution I've been looking for. Now I can seperate the two USB-Keyboards with Linux.
Currently it isn't work because of a NullPointerException in my Project but I got another Error in the EvDev-SrcTest(see Error):

Code: Select all

import java.io.IOException;
import java.io.InputStream;

import com.dgis.input.evdev.EventDevice;
import com.dgis.input.evdev.InputEvent;
import com.dgis.input.evdev.InputListener;



public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new Test(args[0]);
	}

	public Test(String fn) throws IOException{
		System.out.println("Erstelle EventDevice!");
		EventDevice dev = new EventDevice("/dev/input/by-path/platform-3f980000.usb-usb-0:1.5:1.2-event-kbd");
		System.out.println("Erfolgreich!\n");
		
		System.out.println("Erstelle KeyListener!");
		dev.addListener(new InputListener() {
			public void event(InputEvent e) {
				System.out.println("KeyCode: " + e.value);
			}
		});
		System.out.println("Erfolgreich!\n");
		
		System.out.println("Frage Taste ab!");
		InputStream is = new InputStream() {
			public int read() throws IOException {
				return 0;
			}
		};
		System.out.println("Erfolgreich!\n");
		
		System.out.println("Schließe EventDevice!");
		dev.close();
		System.out.println("Erfolgreich!\n");
		
		System.out.println("Beende Programm!");
	}
}

Code: Select all

Exception in thread "Thread-0" java.lang.NullPointerException
	at Test$1.event(Test.java:29)
	at com.dgis.input.evdev.EventDevice.distributeEvent(EventDevice.java:256)
	at com.dgis.input.evdev.EventDevice.access$2(EventDevice.java:253)
	at com.dgis.input.evdev.EventDevice$1.run(EventDevice.java:201)
I hope I can solve this problem, but if you guys have an oppinion to it I would be very thankful.

But really big thanks to all of you! I'm so excited to this project.

@clicky: At the moment the hole code is on my raspBerry because I'm currently test everything before I go further with the main-Project.
I will think about your suggestion and tell you later, all right?

Have all a nice weekend!
JoGGi

UPDATE2: Solved my NullPointerException: e.value isn't allowed...First read then do!
It'se me, JoGGi!
------------------------------------
Learning by doing is my way!
...but everyone needs sometimes some help.

polle0
Posts: 1
Joined: Tue Oct 16, 2018 2:47 pm

Re: [JAVA]KeyEvent - Distinguish between two keyboards

Tue Oct 16, 2018 3:19 pm

Hello friends, I tell you I'm a newbie Raspberry, Java and Linux. I need to use a second mouse connected to my raspberry to measure a displacement. I think that the publication of joggiman could get to work but I find it difficult to try to compile the files "complir.sh". Would you be so kind as to explain me in detail how I should configure everything to be able to operate this code adapted to my second mouse instead of a keyboard?
Thank you very much, greetings from Argentina.

Return to “Java”