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

assign Keyboard keys

Started by hamish1001, 23 March 2012 - 12:00 AM
hamish1001 #1
Posted 23 March 2012 - 01:00 AM
[left]Im wanting to make a program for the turtle to where you have to press certain keys on your keyboard for it to do certain things but i dont know how to assign keys on your keyboard[/left]
:(/>/>
Luanub #2
Posted 23 March 2012 - 02:03 AM
Try using key events


local event, k1 = os.pullEvent("key")
if k1 == 2 then
   do stuff related to the 1 key being pressed
end
hamish1001 #3
Posted 23 March 2012 - 02:40 AM
thanks (how can i change that clueless thing its annoying lol :(/>/> )
Luanub #4
Posted 23 March 2012 - 03:01 AM
Thats your rank, it will change over time based on how active you are on the forums.
hamish1001 #5
Posted 23 March 2012 - 03:19 AM
how long's that? :(/>/>
:)/>/>
:)/>/>
:D/>/>
;)/>/>
:o/>/>
:D/>/>
Ian-Moone #6
Posted 23 March 2012 - 11:28 AM
quite a while
Casper7526 #7
Posted 23 March 2012 - 11:35 AM
50 posts to reach the next rank :(/>/>

Remember though, ranks are just "fun things" they have no reflection of your actual abilities/knowledge.

But at 1000 posts you can request your own title :)/>/>
Wolvan #8
Posted 23 March 2012 - 02:04 PM
50 posts to reach the next rank :(/>/>

Remember though, ranks are just "fun things" they have no reflection of your actual abilities/knowledge.

But at 1000 posts you can request your own title :)/>/>
Niceeeee!
PatriotBob #9
Posted 23 March 2012 - 08:33 PM
So only 980 posts before I can request "Prefers C++"…
Espen #10
Posted 24 March 2012 - 12:33 AM
So only 980 posts before I can request "Prefers C++"…
But please don't trade quality for quantity, just to get a custom title.
Because by the time you've posted hundreds of not-so-useful posts and got your title, people will definitely be much more annoyed about that fact than care for your title. :(/>/>

EDIT: And this was not specifically directed at you, but more a plea to everyone whom it may concern. So no offense!
Edited on 23 March 2012 - 11:35 PM
hamish1001 #11
Posted 24 March 2012 - 01:05 AM
So only 980 posts before I can request "Prefers C++"…
But please don't trade quality for quantity, just to get a custom title.
Because by the time you've posted hundreds of not-so-useful posts and got your title, people will definitely be much more annoyed about that fact than care for your title. :(/>/>

EDIT: And this was not specifically directed at you, but more a plea to everyone whom it may concern. So no offense!

i want a custom title now :)/>/>
Dreossk #12
Posted 18 February 2013 - 02:38 AM
Isn't there a more efficient way to do this? Even for the first key, there is such a delay that my finger is already off the key when the command occurs. If I have many keys to use, that if/elseif is gonna become very slow for the last ones. Can't I directly assign a key like "q=print("hello")?
ChunLing #13
Posted 19 February 2013 - 01:45 AM
If you're referring to this:
Try using key events


local event, k1 = os.pullEvent("key")
if k1 == 2 then
   do stuff related to the 1 key being pressed
end
The code should be plenty fast for a single key assignment. We'd need to see your code to understand why it was so slow. But if you're doing a lot of key assignments, then using a table of functions indexed by key code values would be effective.
Dreossk #14
Posted 19 February 2013 - 01:50 PM
Talking about coding a piano here so I have a IF for letters Q to P for now and it's pretty slow. I think it's the normal ComputerCraft lag when you type so there really isn't much we can do.
MudkipTheEpic #15
Posted 19 February 2013 - 03:39 PM
Isn't there a more efficient way to do this? Even for the first key, there is such a delay that my finger is already off the key when the command occurs. If I have many keys to use, that if/elseif is gonna become very slow for the last ones. Can't I directly assign a key like "q=print("hello")?
<cloudy mode>
No.
</cloudy mode>
theoriginalbit #16
Posted 19 February 2013 - 03:43 PM
Can't I directly assign a key like "q=print("hello")?
I don't know of any good programming language that does it this way.

easiest way

local bindings = {
  ["q"] = function() -- however you play the note q is on end,
  ["w"] = function() -- however you play the note q is on end,
}

while true do
  local event, char = os.pullEvent("char")
  if bindings[char] then
    bindings[char]()
  end
end
ElvishJerricco #17
Posted 19 February 2013 - 04:52 PM
Talking about coding a piano here so I have a IF for letters Q to P for now and it's pretty slow. I think it's the normal ComputerCraft lag when you type so there really isn't much we can do.

The biggest problem is that minecraft only updates the keys twenty times a second. So you can't do more than ten notes a second (one tick for redstone on, one for off).

Anyways this code supports multiple notes being played simultaneously and runs as fast as possible.

Spoiler

local wireOut = 0
local notes = {}
notes[keys.a] = colors.white
notes[keys.s] = colors.orange
notes[keys.d] = colors.magenta
notes[keys.f] = colors.lightBlue
notes[keys.g] = colors.yellow
notes[keys.h] = colors.lime
notes[keys.j] = colors.pink
notes[keys.k] = colors.gray
local queue = {}



local function insert(f)
	table.insert(queue, {co = coroutine.create(f)})
end

local function setTimeout(time, f)
	insert(function()
		sleep(time)
		f()
	end)
	os.queueEvent("justkeepgoing")
end

local function update(k)
	if notes[k] then
		wireOut = bit.bor(wireOut, notes[k])
		setTimeout(0.05, function()
			wireOut = bit.bxor(wireOut, notes[k])
			rs.setBundledOutput("back", wireOut)
		end)
		rs.setBundledOutput("back", wireOut)
	end
end

local function go()
	while true do
		local e,k = os.pullEvent("key")
		update(k)
	end
end

insert(go)
os.queueEvent("justkeepgoing")
while true do
	local e = { os.pullEvent() }
	local dead = {}
	for i,v in ipairs(queue) do
		if v.filter == e[1] or v.filter == nil then
			local ok, filter = coroutine.resume(v.co, unpack(e))
			v.filter = filter
			if coroutine.status(v.co) == "dead" then
				table.insert(dead, i)
			end
		end
	end
	
	for i,v in ipairs(dead) do
		table.remove(queue, v)
	end
end

Note the notes table at the top. notes[<key>] = <wire-color>