How to detect a keypress?

logarithm64

Omnipotent Space Lizard
Joined
Dec 10, 2009
Messages
162
I've been trying to write a function in Python in the CvMainInterface.py file that returns a value of true if a particular key is pressed, but after two hours of swearing at my computer I thought it would be more sensible to see if anyone here had any ideas. I've already had a look at the onKbdEvent function, but I can't seem to copy it into CvMainInterface.py without the interface disappearing from the game. Any help would be greatly appreciated.
 
Here is some code from BUG that detects a keypress (up/down arrow, page up/down, and home/end) on one of the advisor screens. It should be the same for CvMainInterface.

Code:
	def handleInput(self, inputClass):
		if (inputClass.getNotifyCode() == NotifyCode.NOTIFY_CHARACTER):
			func = self.keyFunctionMap.get(inputClass.getData(), None)
			if func:
				if inputClass.getID():
					BugUtil.debug("calling %r", func)
					func()
				return 1
		
		return 0

self.keyFunctionMap is a simple dictionary that maps the key code to the function to call. You can test inputClass.getData() against your key codes using ==, for example:

Code:
if inputClass.getData() == InputTypes.KB_UP:
    ...

Note that you have to use the values from InputTypes instead of strings like "K". You can list all the values in the Python console once you enable cheats with the code "chipotle": Hit ~ (tilde) and enter

Code:
for k in dir(InputTypes): print k

If you have logging enabled in BTS (see my sig) the values will be written to Logs/PythonDbg.log so you can easily reference them or copy-n-paste.
 
Back
Top Bottom