This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
oreillynz's profile picture

Copy & Paste (Multiplayer) from Linux

Started by oreillynz, 05 April 2012 - 11:14 AM
oreillynz #1
Posted 05 April 2012 - 01:14 PM
Playing online?
Can't get Ctrl+V to work correctly for ComputerCraft?
Fighting the urge to scoop your eyes out with a rusty spoon white trying to program in ComputerCraft directly?


Did you know you can get linux to type the contents of the clipboard into minecraft?… Here's how:


First, install some tools:

You'll need to install "xdotool" and "xbindkeys."
You can install these from the command line with "[sudo] apt-get install xdotool, xbindkeys"


Then, copy and paste this script
.. into a sensible location (for example, to "~/autopaste.py)

Spoiler

#!/usr/bin/python
import subprocess, time
key_press_delay_ms = 12
character_mapping = [
	{ "char":'\n', "mapped": 'Return' },
	{ "char":'-', "mapped": 'minus'},
	{ "char":'[', "mapped": 'bracketleft'},
	{ "char":']', "mapped": 'bracketright'},
	{ "char":'{', "mapped": 'braceleft'},
	{ "char":'}', "mapped": 'braceright'},
	{ "char":'\"', "mapped": 'quotedbl'},
	{ "char":'&', "mapped": 'ampersand'},
	{ "char":'/', "mapped": 'slash'},
	{ "char":'\`', "mapped": 'grave'},
	{ "char":'$', "mapped": 'dollar'},
	{ "char":'\'', "mapped": 'apostrophe'},  
]
def flushTyping( arguements ):
	#print("Typing:\n" + arguements )
	time.sleep( key_press_delay_ms * 1.0 / 1000 )
	command = ["xdotool", "type", "--clearmodifiers", "--delay", str( key_press_delay_ms ), str( arguements )]
	subprocess.call(command)
def flushKeypress( arguements ):
	#print("Keypress:\n" + arguements)
	time.sleep( key_press_delay_ms * 1.0 / 1000 )
	arguements = arguements.strip().split(" ")
	#print ["xdotool", "type", "--clearmodifiers", "--delay", key_press_delay_ms ] + arguements
	command = ["xdotool", "key", "--clearmodifiers", "--delay", str( key_press_delay_ms )] + arguements
	subprocess.call( command )
if __name__ == "__main__":
	import pyperclip
	contents = pyperclip.getcb()
	keypress_arguements = ""
	typing_arguements = ""
	last_addition = "typing"
	character_checklist = []
	for item in character_mapping:
		character_checklist.append( item["char"] )
	for character in contents:
		if character in character_checklist:
			for item in character_mapping:
				if item["char"] == character:
					keypress_arguements += " " + item["mapped"]
					break
			if last_addition == 'typing':
				flushTyping( typing_arguements )
				typing_arguements = ""
			last_addition = 'keypress'
		else:
			typing_arguements += character
			if last_addition == 'keypress':
				flushKeypress( keypress_arguements )
				keypress_arguements = ""
			last_addition = 'typing'
	if last_addition == "typing":
		flushTyping( typing_arguements )
	else:
		flushKeypress( keypress_arguements )

You'll need to make that script 'runnable', which assuming you've saved it as something like "~/autopaste.py" can be done (via command line) with "chmod +x autopaste.py"


Grab "Pyperclip" for clipboard access

The excellent "pyperclip" module is required, so you'll need to follow that link, and save "pyperclip.py" in the same location.


Set up configuration file for xbindkeys

Then, get xbindkeys to create a default config file and place it where it'll get automatically loaded using:
"xbindkeys –defaults > $HOME/.xbindkeysrc"

Open up this file, and comment out those examples if you don't want them. We're going to add a command to this config file to run the script you copied when you press "Windows+V". Copy the command below into the file:

"~/autoclip.py"
	Mod4+Super_L+v


Last, watch it run:

Make sure xbindkeys is running (you can also check the current shortcut keys with "xbindkeys -s"), then open up an editor in ComputerCraft (within minecraft), copy some code to the clipboard (just as you would normally), and hit "Windows+V"… you should now see linux doing the frantic typing for you, leaving you to make coffee.



More info + Other notes:

SpoilerChanging the shortcut key
If you want to change the key combination, run "xbindkeys -mk", press the key combination you want, and replace "Mod4+Super_L+v" in the config file with the results that xbindkeys prints out for you.

Changing the typing speed
At the top of "~/autoclip.py" (assuming that is what you saved the script above as), there is a variable "key_press_delay_ms" which is the delay (in milliseconds) between each key press. If you find the typing speed too fast (for example, if latency is causing characters to skip), increase the delay.

It's missing characters as it's typing!
You may have too much lag on your connection for how fast the clipboard is being typed. See "Changing the typing speed" for help with that.
Hawk777 #2
Posted 06 April 2012 - 10:09 AM
Or you can just press CTRL to go into the menu, then press CTRL+V to simultaneously leave the menu and also paste the clipboard.
oreillynz #3
Posted 06 April 2012 - 04:43 PM
You can, but I find when using linux that only the first line gets pasted - even then, the last character is removed.

I suspect it's caused by *nix using single character for line return, where windows uses CR+LF (two characters) - so copying from a linux clipboard ends up with a 'unexpected' error at the end of the first line.

It's slower to use the above method, but at least it works, where-as Ctrl+V doesn't.
libraryaddict #4
Posted 06 April 2012 - 06:08 PM
You know, I was wondering why the linux server hated copy/paste..
Noodle #5
Posted 08 April 2012 - 12:34 AM
Was confused.. Then saw Linux.
TehSomeLuigi #6
Posted 08 April 2012 - 02:11 PM
A fellow Linux user :P/>/>.

Going to bookmark this for reference.


edit: I can't seem to figure out how to work this, but the key combination simply doesn't work..
oreillynz #7
Posted 09 April 2012 - 04:43 AM
Couple of quick questions to help debug….

On the command line, can you check xdotool and xbindkeys are installed? ("xdotool –help" and "xbindkeys –help" should do it)
Did you download and put the "pyperclip.py" file in the same location as the script?
(Oh! Being python, you may need to add an empty "__init__.py" file in the same folder, so python knows it should look in that folder for imports)
If you run "xbindkeys -mk" and press the combination you want, does the output match what you have added to the ~/.xbindkeysrc file?
djblocksaway #8
Posted 10 April 2012 - 02:40 PM
or you could use a program called scar DIVI and use a special code (Not sure if scar DIVI is for linux though :P/>/>)
mrcrocodile123 #9
Posted 29 January 2013 - 07:55 AM
im a tad slow and new to linux, would a video tutorial be possible :/ haha i have a huge code i made on notepad that i wanna put on tekkit server