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

[Lua] Bios.lua keypress section

Started by MudkipTheEpic, 18 December 2012 - 04:35 AM
MudkipTheEpic #1
Posted 18 December 2012 - 05:35 AM
Hello! I'm currently making a program that imitates CraftOS, and I was wondering if there would be any way to implement this part of the bios.lua:


while true do
  local sEvent, param = os.pullEvent()
  if sEvent == "char" then
   sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
   nPos = nPos + 1
   redraw()

  elseif sEvent == "key" then
		  if param == keys.enter then
	-- Enter
	break
  
   elseif param == keys.left then
	-- Left
	if nPos > 0 then
		 nPos = nPos - 1
		 redraw()
	end
  
   elseif param == keys.right then
	-- Right  
	if nPos < string.len(sLine) then
		 nPos = nPos + 1
		 redraw()
	end

   elseif param == keys.up or param == keys.down then
							-- Up or down
	if _tHistory then
		 redraw(" ");
		 if param == keys.up then
		  -- Up
		  if nHistoryPos == nil then
		   if #_tHistory > 0 then
			nHistoryPos = #_tHistory
		   end
		  elseif nHistoryPos > 1 then
		   nHistoryPos = nHistoryPos - 1
		  end
		 else
		  -- Down
		  if nHistoryPos == #_tHistory then
		   nHistoryPos = nil
		  elseif nHistoryPos ~= nil then
		   nHistoryPos = nHistoryPos + 1
		  end  
		 end
	  
		 if nHistoryPos then
										 sLine = _tHistory[nHistoryPos]
										 nPos = string.len( sLine )
									else
		  sLine = ""
		  nPos = 0
		 end
		 redraw()
							end
  
   elseif param == keys.home then
	-- Home
	nPos = 0
	redraw()
   elseif param == keys.delete then
	if nPos < string.len(sLine) then
		 redraw(" ");
		 sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )  
		 redraw()
	end
   elseif param == keys["end"] then
	-- End
	nPos = string.len(sLine)
	redraw()
   end
  end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()
return sLine
end

to be part of my code at: http://www.computerc...e-fake-console/

I'm sorry if I'm just asking for code here, but a pro told me to refer here on this subject.

Thanks, Mudkip
Lyqyd #2
Posted 18 December 2012 - 06:34 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?
MudkipTheEpic #3
Posted 18 December 2012 - 06:38 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?

Wait, what? I just want that function to…. Nevermind, this might be impossible.

Is there any way to implement the history function into a program that just reads the input, not execute it?

Edit: Really, all I want is for the up and down arrow to do the same thing they do in shell, in my program. It would make MUCH more sense if you read my program's thread.
GravityScore #4
Posted 18 December 2012 - 08:41 AM
The shell program (yes it is a program stored on your computer that is run on startup) can be found in rom/programs/shell.

The shell program uses the read function to read a line from the user, and then executes it.
It achieves the history feature by using the history feature of the read function.

Demonstration on how to use the read feature:

historyTable = {}
while true do
  term.clear()
  term.setCursorPos(1, 1)
  readLine = read(nil, historyTable)
  table.insert(historyTable, readLine)
end

This should allow you to use the up and down arrow keys to move between previously typed things.
Hope this helps :P/>
MudkipTheEpic #5
Posted 18 December 2012 - 08:59 AM
Thank you so much! Updating program! :D/>
theoriginalbit #6
Posted 18 December 2012 - 11:42 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?

read is terminate-able. maybe he doesn't want it terminate-able.
Doyle3694 #7
Posted 18 December 2012 - 12:30 PM
if so, the originalBIT, pcall would be a much better way
theoriginalbit #8
Posted 18 December 2012 - 12:31 PM
pcall….

yes you can use pcall. but he didnt say to use pcall with read, he said use read.
Doyle3694 #9
Posted 18 December 2012 - 12:35 PM
pcall….

yes you can use pcall. but he didnt say to use pcall with read, he said use read.

Anyways, that's not the subject, since that wasn't the reason. Back to topic! ;)/>
MudkipTheEpic #10
Posted 18 December 2012 - 01:21 PM
Thank you so much! Updating program! :D/>
Well it worked, so it is resolved now. :D/>


pcall….

yes you can use pcall. but he didnt say to use pcall with read, he said use read.

Anyways, that's not the subject, since that wasn't the reason. Back to topic! ;)/>

Don't worry, used os.pullEvent = os.pullEventRaw to secure it. ;)/>