One USB device on two PC's? - HELP NEEDED - Page 1
Forum section: electronics / beginners

author: Xephlon
author_rank: Newbie
timestamp: October 31, 2019, 09:04:48 pm
content: Hey there,Recently I have encountered an issue with a personal project where I need to connect a keyboard to two PC's. I am tempted to fabricate a Female to 2 Male USB Y-splitter, however I am not very confident with electronics and I am worried that either the two 5V outputs from the PC's will fry my keyboard, or the data signal from the keyboard will decay when being split and will not input correct values. I have found keyboard switches to switch between two PC's, however I want simultaneous inputs. Sorry if this is a rather irrelevant topic for this forum, however I hope you guys can give some advice and tips about the 5V outputs and the data signals.Many thanks to all who read & respond,Josh

author: ataradov
author_rank: Super Contributor
timestamp: October 31, 2019, 09:25:28 pm
content: It is impossible to share the same USB device between the two computers. USB stack is stateful, there is an actual enumeration process that is going on, you can't have the same device be enumerated on two hosts.This will not make any sense electrically too.

author: 0culus
author_rank: Super Contributor
timestamp: October 31, 2019, 09:27:16 pm
content: You need a KVM switch, a/k/a Keyboard, Video, Mouse switch. One enables you to share a monitor, keyboard, and a mouse between at least 2 computers. You do have to switch it back and forth, but it's still way more convenient than having duplicate peripherals on the bench.

author: boffin
author_rank: Unknown
timestamp: October 31, 2019, 09:32:11 pm
content: Quote from: Xephlon on October 31, 2019, 09:04:48 pmYou can buy little switches like that. I have one connected to my (shared) printer/scanner so it's connected to either my or my wife's computer.

author: 0culus
author_rank: Super Contributor
timestamp: October 31, 2019, 09:36:26 pm
content: The bottom line is that ataradov is right; I don't know of any way to do simultaneous inputs to two computers.

author: Xephlon
author_rank: Newbie
timestamp: October 31, 2019, 10:26:03 pm
content: Okay. Its annoying that I cant have them run simultaneously, but ill find a solution.Cheers!

author: jhpadjustable
author_rank: Frequent Contributor
timestamp: October 31, 2019, 11:12:17 pm
content: Sounds a bit like a keylogger with an extra USB device interface. That could get complicated in hardware, especially in the event the two PCs try to command different HID configurations. Maybe it would be easier to do it in one host PC's software, by capturing keypresses on the host PC and forwarding them to the other host PC by whatever protocol happens to be useful. (Something along the lines ofUSB/IPor some other flavor of event injector, perhaps?)

author: wraper
author_rank: Unknown
timestamp: October 31, 2019, 11:19:23 pm
content: You can use software solutionhttps://symless.com/synergy

author: jackthomson41
author_rank: Regular Contributor
timestamp: November 01, 2019, 12:46:20 am
content: USB is a slave and your computer is host. You can't connect single slave to multiple host but can do the reverse. simple logic ofEmbedded Systemscomponents.

author: andycsmith
author_rank: Contributor
timestamp: November 01, 2019, 11:35:15 am
content: The question is why do you need one keyboard plugged into two PC's, you want the same characters to appear at the same time on both PC's.Are you trying to test some software or replicate actions in multiple environments, if so there is software that will allow you to script this and play back as if you are physically typing on the keyboard.

author: rstofer
author_rank: Super Contributor
timestamp: November 01, 2019, 03:56:37 pm
content: Quote from: Xephlon on October 31, 2019, 10:26:03 pmIn general, I don't think it is possible either. HOWEVER... Since it is a keyboard and therefore receives very little information back from the PC...If you ran the keyboard to some kind of microprocessor with 3 USB ports, I don't see why code couldn't be written that would do all of the arbitration. It's easy, generally, to become a USB HID device and if we're starting with a HID device (keyboard) then it should be straightforward. One needs to figure out which PC sends what to the device but having the device send to multiple PCs should be easy.Since it is a keyboard, it should be easy to get at it in userspace under Linux.I don't know anything about writing Linux drivers but the Raspberry Pi was 4 USB ports. I'm not even sure that a driver is required. It seems more like an application with 1 keyboard input and two keyboard outputs. It could be pretty straightforward...

author: rstofer
author_rank: Super Contributor
timestamp: November 01, 2019, 04:58:40 pm
content: We would want Linux to create two USB HID output devices and listen to one input HID device. The listen part is probably pretty easy, it's a keyboard. Creating the two HID devices might take some research like:https://developer.ridgerun.com/wiki/index.php?title=Linux_USB_gadget_(g_hid)Google around for 'make linux look like HID device' or something similar.I don't pretend to know how to do it but I'm getting more certain it is possible every minute.

author: RoGeorge
author_rank: Super Contributor
timestamp: November 01, 2019, 05:12:29 pm
content: Quote from: Xephlon on October 31, 2019, 09:04:48 pmAt first look that seems like a spying device (e.g. to steal what is typed at somebody else's computer, like passwords, credit cards numbers, emails and so on), and that would be illegal.Care to explain why such a device is needed for your personal project?

author: Ian.M
author_rank: Super Contributor
timestamp: November 01, 2019, 05:25:04 pm
content: Quote from: rstofer on November 01, 2019, 03:56:37 pmYou are on the right track, but it would need two USB device ports and one USB host port. The Pi 4 has four host ports and one OTG port which can be used as a device port, so could concentrate up to four keyboards to one PC, not the other way round. As MCUs with multiple device or OTG ports are rarer than hen's teeth, either you need multiple MCUs with a comms bus between them, each handling one port, or possibly a FPGA that can support multiple USB 'engines'.

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: November 01, 2019, 06:00:31 pm
content: You can do that easily with one rpi and two arduino leonardos.https://www.arduino.cc/en/Main/Arduino_BoardLeonardo. Connect the keyboard to the rpi, the leonardos to the PCs, and forward via the serial port (or ports) the characters to the leonardos.

author: rstofer
author_rank: Super Contributor
timestamp: November 01, 2019, 06:08:00 pm
content: It's dead simple to use something like RawHID in a Teensy to create a HID device. I did that some years back when I wanted to add knobs and dials to Microsoft Flight Simulator.https://www.pjrc.com/teensy/rawhid.htmlSo, easiest case, a couple of Teensy's should do the output side.There is a USB Host Shield available for Arduino which will work with the Teensy so, maybe a third Teensy to work as the host and accept input from a keyboard and disperse it to the other Teensy's.https://www.pjrc.com/teensy/td_libs_USBHostShield.htmlI can't find the Host Shield mentioned in the above link but I can find others at Adafruit or Sparkfun. That may require some changes in the library code but I have no idea how difficult that would be. Probably not very...Yes, this can definitely be done. My solution, with 3 Teensy's and a USB Host Shield is obscene in BOM. No doubt there is an easier way to do it but it's not really my project.I am pretty convinced it can be done.

author: magic
author_rank: Super Contributor
timestamp: November 01, 2019, 09:11:38 pm
content: Try a PS/2 keyboard connected to two USB-PS/2 dongles.It may have some chance of working, depending on how the dongles handle unexpected bytes produced by the other dongle when it initializes the keyboard.

author: Bicurico
author_rank: Super Contributor
timestamp: November 01, 2019, 10:30:47 pm
content: @magic: that was my thought, too.Older wireless keyboards and mice had the problem that you could not operate several of them in viccinity.Just get two sets of old wireless keyboards and plug the two dongles into your computers, using then only one keyboard. That should work...Regards,Vitor

author: soldar
author_rank: Super Contributor
timestamp: November 01, 2019, 11:09:39 pm
content: With the old AT PS2 keyboards this was very easy to do because besides the +Vcc and GND there were just two wires, data and clock.

author: Nominal Animal
author_rank: Super Contributor
timestamp: November 02, 2019, 03:39:54 pm
content: If the PC's run Linux, you only need two trivial daemons forwarding the keypresses from one to the other.The basic code is trivial; it only becomes harder if you want to secure the connection (using OpenSSL or similar).If you want, I can show you the basic daemon C code. This easily scales to more than two machines, and more than two keyboards, as well.If you want a hardware solution, useN+1 microcontrollers with a native USB interface, withNdevices, and one host.(You can do withNmicrocontrollers, if one of the ones with a native USB client interface also has an USB host interface.)OnN-1 USB connections to the PCs, use an USB isolator, to avoid ground loops. The cheap eBay ones work well for max. 12Mbit/s USB, which is perfectly fine for keyboards and mice and many USB audio devices. If this is used strictly with laptops on battery power, you can omit the isolators.I would probably useNPro Micros (same ATmega32u4 as in Leonardos, but much smaller board; the 5V/16MHz model, not 3.3V/8MHz), one USB host shield, andNADuM4160/3160-based USB isolators, because I know these work well together (except I haven't used any of the USB host shields). On fleabay, pro micros and the USB host shield cost about 4€ apiece, and the USB isolators about 8€ apiece. ForN=2, this comes to about 25€ or so.If you can use a PS/2 keyboard, then you don't need to use the USB host shield. You can use thePS2Keyboardlibrary with Pro Micros, because they use the same ATmega32u4 microcontroller as Teensy 2.0.The idea is that the microcontrollers have a shared ground, and exchange key press information using UART, SPI, or I2C. For a pair of microcontrollers, the UART makes the most sense.

author: TomS_
author_rank: Frequent Contributor
timestamp: November 02, 2019, 07:29:45 pm
content: Quote from: wraper on October 31, 2019, 11:19:23 pmAnother recommendation for Synergy. I use it between two Macs to share a keyboard and mouse (and clipboard!), but you can use it between any combination of supported OSes.