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

How to make Custom Cursors (Like in my WinOS)

Started by ComputerCraftFan11, 06 April 2012 - 02:13 AM
ComputerCraftFan11 #1
Posted 06 April 2012 - 04:13 AM
If you wan't to make a cursor, you need term.setCursorPos() and some variables. Here is a example:

local currentX = 1
local currentY = 1
These will set the cursor position.

function drawCursor()
  term.clear()
  term.setCursorPos(currentX, currentY)
  write(">")
end
This will draw the cursor at the current x/y, now that we got that, we need to call drawCursor() and add controls like this:


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

If you put all this code together like this:


local currentX = 1 --Saves the current X of the cursor
local currentY = 1 --Saves the current Y of the cursor


function drawCursor()
  term.clear() --Clears the screen
  term.setCursorPos(currentX, currentY)  --Draws a cursor at the current X/Y
  write(">")
end


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

It will create a cursor that can move left and right.

If you wan't to make options in a menu, remove the left and right and keep up/down and make it so if the cursor is at the same position of the button, make it open the button.

This is how I did it in my WinOS. You can look at that source for more information.

To change the cursor, in drawCursor(), edit write(">") and change it to write("your cursor")
Noodle #2
Posted 08 April 2012 - 12:33 AM
I tried this, Thanks!
justync7 #3
Posted 10 April 2012 - 10:59 PM
Thanks I Used This In My New OS. Find It Here
djblocksaway #4
Posted 11 April 2012 - 05:46 AM
If you wan't to make a cursor, you need term.setCursorPos() and some variables. Here is a example:

local currentX = 1
local currentY = 1
These will set the cursor position.

function drawCursor()
  term.clear()
  term.setCursorPos(currentX, currentY)
  write(">")
end
This will draw the cursor at the current x/y, now that we got that, we need to call drawCursor() and add controls like this:


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

If you put all this code together like this:


local currentX = 1 --Saves the current X of the cursor
local currentY = 1 --Saves the current Y of the cursor


function drawCursor()
  term.clear() --Clears the screen
  term.setCursorPos(currentX, currentY)  --Draws a cursor at the current X/Y
  write(">")
end


while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
	currentY = currentY -1
  elseif key == 31 or key == 208 then --down
	currentY = currentY +1
  elseif key == 203 or key == 30 then --left
	currentX = currentX -1
  elseif key == 205 or key == 32 then --right
	 currentX = currentX +1
  end
end

It will create a cursor that can move left and right.

If you wan't to make options in a menu, remove the left and right and keep up/down and make it so if the cursor is at the same position of the button, make it open the button.

This is how I did it in my WinOS. You can look at that source for more information.

To change the cursor, in drawCursor(), edit write(">") and change it to write("your cursor")
o.O i might try this later
BaconHawk101 #5
Posted 08 March 2013 - 02:17 PM
Awesome
superaxander #6
Posted 09 March 2013 - 09:29 AM
You can probably use term.getCursorPos() for this

currentx, currenty = term.getCursorPos()
Icabob4 #7
Posted 09 March 2013 - 10:19 AM
let's see…If I use copy and paste I could have a ☺ as my cursor. ☺= :)/> …and since it's linux, another way to do it would be to use

\xe2\x98\xba
instead of the smile
Pharap #8
Posted 09 March 2013 - 11:04 AM
Was expecting a tutorial on .Cur files lol
Shnupbups #9
Posted 09 March 2013 - 12:23 PM
THE NECROMANCY!!
Dlcruz129 #10
Posted 09 March 2013 - 12:40 PM
THE NECROMANCY!!

I don't mind tutorial necromancy.
no9name909 #11
Posted 13 March 2013 - 07:17 AM
Nice tutorial. However, when you use term.clear() the whole screen will clear.
How do you move the cursor without clearing the screen?
Pharap #12
Posted 13 March 2013 - 03:55 PM
Nice tutorial. However, when you use term.clear() the whole screen will clear.
How do you move the cursor without clearing the screen?


term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.
Shnupbups #13
Posted 13 March 2013 - 07:29 PM

term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.
He's not THAT stupid, he's asking how do you remove the previous cursor from the screen without clearing the entire screen. And he probably wouldn't like term.clearLine() either, because that still clears the entire line.
Pharap #14
Posted 14 March 2013 - 04:42 AM

term.setCursorPos(newX, newY)
Swap out newX for the X coordinate of the new position and newY for the Y coordinate of the new position.
IE term.setCursorPos(4,5) would move the cursor to the 4th column and the 5th row, and the next time you do term.write, whatever you write gets written there.
He's not THAT stupid, he's asking how do you remove the previous cursor from the screen without clearing the entire screen. And he probably wouldn't like term.clearLine() either, because that still clears the entire line.
"Whatever you write gets written there" hence if you write an empty space, it will remove the previous cursor.
Regardless, not being experienced with programming does not make someone 'stupid'. Learning to read code is a like learning a new language and struggling with it or not understanding what bits are doing what does not mean someone is stupid, it means they are inexperienced.
no9name909 #15
Posted 14 March 2013 - 08:43 AM
When I looked in the lua manual I saw that \b represents a backspace. Does this also work with CC?
If it does, it can be used to remove the cursor without clearing the whole screen.
Pharap #16
Posted 14 March 2013 - 09:12 AM
When I looked in the lua manual I saw that \b represents a backspace. Does this also work with CC?
If it does, it can be used to remove the cursor without clearing the whole screen.

You can just write over top of the cursor and it will replace the cursor(">").
eg:

if key == 17 or key == 200 then --up
term.setCursorPos(currentX, currentY)
term.write(" ")--clears previous > 
	    currentY = currentY -1
  elseif key == 31 or key == 208 then --down