:(/>/>
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
assign Keyboard keys
Started by hamish1001, 23 March 2012 - 12:00 AMPosted 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]
:(/>/>
:(/>/>
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
Posted 23 March 2012 - 02:40 AM
thanks (how can i change that clueless thing its annoying lol :(/>/> )
Posted 23 March 2012 - 03:01 AM
Thats your rank, it will change over time based on how active you are on the forums.
Posted 23 March 2012 - 03:19 AM
how long's that? :(/>/>
:)/>/>
:)/>/>
:D/>/>
;)/>/>
:o/>/>
:D/>/>
:)/>/>
:)/>/>
:D/>/>
;)/>/>
:o/>/>
:D/>/>
Posted 23 March 2012 - 11:28 AM
quite a while
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 :)/>/>
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 :)/>/>
Posted 23 March 2012 - 02:04 PM
Niceeeee!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 :)/>/>
Posted 23 March 2012 - 08:33 PM
So only 980 posts before I can request "Prefers C++"…
Posted 24 March 2012 - 12:33 AM
But please don't trade quality for quantity, just to get a custom title.So only 980 posts before I can request "Prefers C++"…
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
Posted 24 March 2012 - 01:05 AM
But please don't trade quality for quantity, just to get a custom title.So only 980 posts before I can request "Prefers C++"…
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 :)/>/>
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")?
Posted 19 February 2013 - 01:45 AM
If you're referring to this:
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.Try using key eventslocal event, k1 = os.pullEvent("key") if k1 == 2 then do stuff related to the 1 key being pressed end
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.
Posted 19 February 2013 - 03:39 PM
<cloudy mode>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")?
No.
</cloudy mode>
Posted 19 February 2013 - 03:43 PM
I don't know of any good programming language that does it this way.Can't I directly assign a key like "q=print("hello")?
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
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>