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

Chatkeyboard

Started by Wilma456, 21 June 2017 - 04:53 PM
Wilma456 #1
Posted 21 June 2017 - 06:53 PM
This is a Keyboard in the Chat!

Note: Don't klick the letters too fast!

Screenshot

Installation
wget https://raw.githubusercontent.com/Wilma456/Computercraft/master/Command/chatkeyboard.lua chchatkeyboard
Or through Packman

How it worksEvery Char has a block. If you click the char, it place a block in hight 255 over the command computer. The Program check which block is at this position
Wilma456 #2
Posted 22 June 2017 - 06:28 PM
Update: Added Ctrl
SquidDev #3
Posted 22 June 2017 - 06:42 PM
Neat idea! I remember implementing a very basic version of this in vanilla command blocks in one of my many failed projects :)/>.

One thing which might be worth considering is using the /trigger command and monitoring the scoreboard. This way the user doesn't have to have admin permissions to use it (and you don't have to worry about assigning a different block for each letter). You might also want to use commands.execAsync where appropriate, as that doesn't block the program, and so should make the keyboard more responsive.
magiczocker #4
Posted 16 September 2017 - 10:24 AM
I improved your programm a little bit:
- The program no longer place blocks at y=255
- The keyboard is way faster
Spoiler

if not commands then
  error("Please use an command computer. If you use an older version of CC please use CC 1.7 or newer.")
end
local id=os.getComputerID()
local keynormal={"1","2","3","4","5","6","7","8","9","0","backspace","q","w","e","r","t","z","u","i","o","p","enter","capsLock","a","s","d","f","g","h","j","k","l","up","down","space","y","x","c","v","b","n","m","left","right","tab","rightCtrl"}
local capskey={"!",'\\"',"?","$","%","&","/","(",")","=","backspace","Q","W","E","R","T","Z","U","I","O","P","enter","capsLock","A","S","D","F","G","H","J","K","L","up","down","space","Y","X","C","V","B","N","M","left","right","tab","leftCtrl"}
local testcaps=false
local commandCheck=commands.exec('/trigger keyboard'..id..' set 0')
local function clearChat()
  local _=""
  for i=1,99 do
    _=_.."																			   "
  end
  commands.exec('tellraw @p "'.._..'"')
end
local function createKeyboard()
  local text='/tellraw @p [""'
  for i=1,#keynormal do
    text=text..',{"text":"'
    if testcaps then
	  text=text..capskey[i]
    else
	  text=text..keynormal[i]
    end
    text=text..'","color":"blue","clickEvent":{"action":"run_command","value":"'..(commandCheck and ('/trigger keyboard'..id..' set '..i) or ('/scoreboard players set @p keyboard'..id..' '..i))..'"}},{"text":" "}'
    if i==11 or i==23 or i==35 or i==46 then
	  text=text..']'
	  commands.exec(text)
	  text='/tellraw @p [""'
    end
  end
end
commands.exec("scoreboard objectives remove keyboard"..id)
if commandCheck then
  commands.exec("scoreboard objectives add keyboard"..id.." trigger")
else
  commands.exec("scoreboard objectives add keyboard"..id.." dummy")
end
commands.exec("gamerule sendCommandFeedback false")
commands.exec("gamerule commandBlockOutput false")
commands.exec("gamerule logAdminCommands false")
if commandCheck then
  commands.exec("scoreboard players enable @p keyboard"..id)
end
clearChat()
createKeyboard()
local a=function()
  while true do
    if commands.exec("testfor @p[score_keyboard"..id.."_min=1]") then
	  local _success,_output=commands.exec("scoreboard players list @p")
	  if _success and #_output>1 then
	    for i=2,#_output do
		  if string.sub(_output[i],3,string.find(_output[i],":")-1)=="keyboard"..id then
		    local _=tonumber(string.sub(_output[i],string.find(_output[i],":")+2,string.find(_output[i],"%(")-2))
		    if type(_)=="number" and keynormal[_] then
			  if testcaps then
			    if #capskey[_]==1 then
				  os.queueEvent("key",keys[string.lower(capskey[_])])
			    else
				  os.queueEvent("key",keys[capskey[_]])
			    end
			  else
			    if #keynormal[_]==1 then
				  os.queueEvent("key",keys[string.lower(keynormal[_])])
			    else
				  os.queueEvent("key",keys[keynormal[_]])
			    end
			  end
			  if #keynormal[_]==1 then
			    if testcaps then
				  os.queueEvent("char",capskey[_])
			    else
				  os.queueEvent("char",keynormal[_])
			    end
			  elseif keynormal[_]=='\\"' then
			    os.queueEvent("char",'"')
			  elseif keynormal[_]=='space' then
			    os.queueEvent("char",' ')
			  end
			  oldcaps=testcaps
			  testcaps=(keynormal[_]=="capsLock" and not testcaps) or false
			  if testcaps~=oldcaps then
			    clearChat()
			    createKeyboard()
			  end
		    end
		    if commandCheck then
			  commands.exec("scoreboard players reset @p keyboard"..id)
			  commands.exec("scoreboard players enable @p keyboard"..id)
			  clearChat()
			  createKeyboard()
		    else
			  commands.exec("scoreboard players set @p keyboard"..id.." 0")
			  clearChat()
			  createKeyboard()
		    end
		    break
		  end
	    end
	  end
    end
    sleep(0)
  end
end
local b=function()
  if fs.exists("rom/programs/shell") then
    local ok,err=pcall(loadfile("rom/programs/shell"))
  else
    local ok,err=pcall(loadfile("rom/programs/shell.lua"))
  end
end
parallel.waitForAny(a,B)/>
Edited on 02 September 2018 - 12:43 PM