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

detect ctrl - s and then reboot?

Started by Goof, 02 June 2013 - 12:08 PM
Goof #1
Posted 02 June 2013 - 02:08 PM
Hello

im trying to make my program "Advlock" even more secure. im trying to reboot the computer, when the computer keys "ctrl s" are typed… but it doesnt actually work as i suspected…
is it because ctrl s are hardcoded into the program?



os.pullEvent = os.pullEventRaw
while true do
	print("Eventing...")
	event, key, id = os.pullEvent()
	if event == "key" then
		print("Key detected...")
		if (key == keys.rightCtrl or key == keys.leftCtrl) and key == keys.s or key == keys.r then
			print("Rebooting...") sleep(0.5)
			os.reboot()
		end
	end
end	


Thanks In Advance -
Engineer #2
Posted 02 June 2013 - 02:37 PM
Ctrl + s is a forced shutdown, this is coded in the java, and you cant really overwrite it. If you want a shortcut for rebooting the comp, use ctrl + r, woch is also coded in the java :P/>
MKlegoman357 #3
Posted 02 June 2013 - 03:02 PM
Well,
You check if the key is "ctrl" and then check if it`s "s". It 's like you check "if dog == dog and dog == cow". You can't do it like that. If you want to check multiple keys then you have to check if "ctrl" key is pressed and then wait for 0.9 seconds and if in that time "s" is pressed then you can say that someone wants to shutdown the computer and then you reboot it before it shutdown. There is an API for "ctrl" key presses, I don't remember it's name, but it's developer made "goroutine" API too, search for it. Hope that was useful.
Yevano #4
Posted 02 June 2013 - 03:09 PM
Your problem is that this condition will never be true: (key == keys.rightCtrl or key == keys.leftCtrl) and key == keys.s or key == keys.r. key cannot be equal to two values. As far as I know, there is no way to get a key state except from events, so I'm not sure if it's really possible to check if two keys are pressed at once. You can, however, check if the events are fired close together by using a timer.
GopherAtl #5
Posted 02 June 2013 - 03:18 PM
there's a trick to detect if ctrl was held down when certain keys are pressed, specifically the keys that can be typed (a-z, 0-9, punctuation, etc). Normally, you get a key event, followed immediately by a char event. When holding ctrl, you will get the key event, but not the char event. I've got an api, ctrlkeys, in my APIs thread in the APIs and Utilities section, here, which helps to identify these and generates additional ctrl_key events when a detectable ctrl+key combination is pressed.