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

CC Keyboard V1.1

Started by jplee, 21 August 2015 - 01:10 AM
jplee #1
Posted 21 August 2015 - 03:10 AM
This APi is formated around how Lwjgl does their key input functions but with a few additons and tweaks.
To get the API: pastebin get YeKip4Vi keyboard
Version 1.1

While I was tinkering on other Computercraft projects, I found that I was in need of a way to test if multiple keys were down or not. I then started to look around, I found one but it did not have the functionality that I wanted. So I decided to create one for my self. I then decided to share this API because I liked how nice it worked.

Implementing into your project
To have this api in your project you will need to have this segment of code

keyboard = dofile("keyboard")

This API will overrite os.pullEvent(), so in the code there is a way to toggle this on or off.

Documentation
Spoilerkeyboard.update( event, p2, p3, p4, p5, p6 )
This updates the keyboard API, this will happen automaticaly unless you disable the overrite.
Input from os.pullEvent() is required for this API to function if overrite is not enabled.

keyboard.isKeyDown( nKey )
Returns a boolean value for whether it is being pressed and an integer for how long it has been pressed.
This API is designed to work with the keys API. Example: keyboard.isKeyDown(keys.a)

keyboard.enableRepeatEvents( bEnable )
Enables repeat keys. If repeat events are enabled, key input will repeat after the system assigned time is reached.
Repeat events are enabled at the start.

keybaord.areRepeatKeysEnabled( )
Returns a boolean if repeat keys are enabled.

keyboard.getKeyIndex( sKey )
Returns a integer for the index value of sKey or -1 if skey is not found.

keyboard.getKeyName( nKey )
Returns a string for the key name of nKey or nil if nkey is not found.

keyboard.getEventKey( )
Returns a integer for the current keys index value.

keyboard.getEventKeyState( )
Returns a boolean value for if the current key is being pressed.

keyboard.getEventKeyActiveTime( )
Returns a integer for the current key for how long it was pressed.

keyboard.getEventMillisecnds( )
Returns a integer for the currnet key for the time it was pressed.

keyboard.isRepeatEvent( )
Returns a boolean for the current key for whether the key is a repeat event.

keyboard.restore( )
Restores the original os.pullEvent(). This should be called if you are done using this API.
If you have prevented the overrite do not use this.

Example Code
Spoiler

keyboard = dofile("keyboard")

keyboard.enableRepeatEvents(true)
timer = os.startTimer(0.0)
while true do
  event, p2 = os.pullEvent()

  if keyboard.getEventKeyState() and (keyboard.getEventActiveTime() <= 0.1 or keyboard.isRepeatEvent()) then
	term.setCursorPos(1,18)
	term.write("Key:"..keyboard.getEventKey())
	
	if keyboard.getEventKey() == keys.q and keyboard.isKeyDown(keys.leftCtrl) then
	  break
	end
  else
	term.setCursorPos(1,18)
	term.clearLine()
  end

  if keyboard.isKeyDown(keys.w) and keyboard.isKeyDown(keys.a) and keyboard.isKeyDown(keys.s) and keyboard.isKeyDown(keys.d) then
	term.setCursorPos(1,19)
	term.write("You are pressing wasd")
  else
	term.setCursorPos(1,19)
	term.clearLine()
  end

  if event == "timer" and p2 == timer then
	timer = os.startTimer(0.1)
  end
end

Update Log
Spoilerv1.1 - Fixed bug with repeat events
v1.0 - Release
Edited on 22 August 2015 - 07:19 AM
Yarillo #2
Posted 21 August 2015 - 06:46 AM
Very nice, I was looking for something like that a while ago.

*saves*
Edited on 23 August 2015 - 06:53 AM
jplee #3
Posted 21 August 2015 - 08:41 AM
Very nice, I was looking for something like that a while ago.
Thanks, glad it was helpful.