Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Question

 Hi there,

I have this problem with key press event. For some random reason it works just kind of, well, it works, but my code doesn't. I am using multiple functions to du stuff that depends in certain keys pressed, but that is some sort of weird random error there in between 'keypress' and 'menuselect' function. Could someone take a look please? (I will definitly send some online cookie for ya)

 

---init
component=require('component')
gpu=component.gpu
gpu.setDepth(8)
term=require('term')
term.clear()
event=require('event')
unicode=require('unicode')
colors=require('colors')
key=require('keyboard')
local c=''
local character=''
local a=1
local b=1
local sub=0
local ref=0

---settings default fillment of GUI elements
local boxoutfilluni=0x2588
local boxoutfillunitop=0x2580
local boxoutfillunibot=0x2584
local boxoutfilltex='#'
local boxfilluni=0x2588
local boxfilltex='#'

local w,h=gpu.getResolution()
local exitnum=0
local function boxout(x,y,w,h,unic,fore,back)
	if back==nil then
		back=0x000000
	end
	if fore==nil then
		fore=0xffffff
	end
	gpu.setForeground(fore)
	gpu.setBackground(back)
	if unic==1 then
		gpu.fill(x,y,w,1,unicode.char(boxoutfillunitop))
		gpu.fill(x,h,w,1,unicode.char(boxoutfillunibot))
		gpu.fill(x,y,1,h,unicode.char(boxoutfilluni))
		gpu.fill(w,y,1,h,unicode.char(boxoutfilluni))
	else
		gpu.fill(x,y,w,1,tostring(boxoutfilltex))
		gpu.fill(x,h,w,1,tostring(boxoutfilltex))
		gpu.fill(x,y,1,h,tostring(boxoutfilltex))
		gpu.fill(w,y,1,h,tostring(boxoutfilltex))
	end
end
local function textXY(x,y,text,col,fg,bg)
	if col==1 then
		if bg==nil then
			bg=0x000000
		end
		if fg==nil then
			fg=0xFFFFFF
		end
		gpu.setForeground(fg)
		gpu.setBackground(bg)
		gpu.set(x,y,tostring(text))
		gpu.setForeground(0xFFFFFF)
		gpu.setBackground(0x000000)
	else
		gpu.setForeground(0xFFFFFF)
		gpu.setBackground(0x000000)
		gpu.set(x,y,tostring(text))
	end
end
local function box(x,y,w,h,col,fg,bg)
if col==1 then
		if bg==nil then
			bg=0x000000
		end
		if fg==nil then
			fg=0xFFFFFF
		end
		gpu.setForeground(fg)
		gpu.setBackground(bg)
		gpu.fill(x,y,w,h,unicode.char(boxfilluni))
		gpu.setForeground(0xffffff)
		gpu.setBackground(0x000000)
	else
		gpu.setForeground(0xffffff)
		gpu.setBackground(0x000000)
		gpu.fill(x,y,w,h,unicode.char(boxfilluni))
	end
end
local function keypress(character)
	if character==203 then
		if a>1 then
			a=a-1
			ref=1
		elseif a==1 then
			a=1
		end
		textXY(2,3,'Left ')
	end
	if character==205 then
		if a<6 then
			a=a+1
			ref=1
		elseif a==6 then
			a=6
		end
		textXY(2,3,'Right')
	end
	if character==200 then
		b=b+1
		ref=1
		textXY(2,3,'Up   ')
	end
	if character==208 then
		b=b-1
		ref=1
		textXY(2,3,'Down ')
	end
	if ref==1 then
		menuselect(a,b)
		ref=0
	end
	if (character==28)and(a==6) then
		textXY(2,3,'Exiting system. CYA')
		os.sleep(1)
		term.clear()
		box(1,1,w,h,1,0x000000)
		os.exit()
	end
end
local function menuselect(a,b)
	if (a==1)and(b==1) then
		textXY(2,2,'|File|',1,0xFF0000,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
		textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
		textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)
	elseif (a==2)and(b==1) then
		textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0xFF0000,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
		textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
		textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)
	elseif (a==3)and(b==1) then
		textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0xFF0000,0xBBBBBB)
		textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
		textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)
	elseif (a==4)and(b==1) then
		textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
		textXY(24,2,'|Help|',1,0xFF0000,0xBBBBBB)
		textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)
	elseif (a==5)and(b==1) then
		textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
		textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
		textXY(30,2,'|Info|',1,0xFF0000,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)
	elseif (a==6)and(b==1) then
		textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
		textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
		textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
		textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
		textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
		textXY(36,2,'|Exit|',1,0xFF0000,0xBBBBBB)
	end
end

boxout(1,1,w,h,1)
box(2,2,w-2,1,1,0xBBBBBB)
textXY(2,2,'|File|',1,0x6699FF,0xBBBBBB)
textXY(8,2,'|Apps|',1,0x6699FF,0xBBBBBB)
textXY(14,2,'|Settings|',1,0x6699FF,0xBBBBBB)
textXY(24,2,'|Help|',1,0x6699FF,0xBBBBBB)
textXY(30,2,'|Info|',1,0x6699FF,0xBBBBBB)
textXY(36,2,'|Exit|',1,0x6699FF,0xBBBBBB)

menuselect(a,b)

while true do
	local _,_,_,ch=event.pull('key_down')
	sub=sub+1
	keypress(ch)
	textXY(2,3,ch..'    ')
	textXY(2,4,sub)
end

 

Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.