slightly simpler version. Also with formatting fixed.
While that is better, it's a rather odd formatting style, and inconsistent in a few places.
Explaining the problem in just a little bit more detail (if you can't figure it out yourself) - the function is called both when you press the key, and when you release the key, and there's an argument passed to indicate whether you're pressing or releasing it. So like in the above post, you need to check the value of it.
As for using this - please don't. It's fine as a learning exercise, but don't use it beyond one test, and make sure you're not in a server with people who get really annoyed by it.