Skip to content

[Tutorial] 3. Hooking Keys

Heloo88 edited this page Jan 28, 2012 · 7 revisions

Tools

  • Terraria Source
  • ModClass

Adding the Hook

  • Open Player.cs
  • Navigate to Keys[] pressedKeys = Main.keyState.GetPressedKeys(); [Line 3003]
  • Add ModClass.onKeys(pressedKeys); below it
  • Open ModClass.cs
  • Add the following declaration using Microsoft.Xna.Framework.Input. (or using Microsoft.XNA.Framework.Input)
  • Add the following variable private static Keys[] previousKeys;
  • Add the following function public static void onKeys(Keys[] pressedKeys) { previousKeys = pressedKeys; }

Using the Hook

  • Change the following example to your needs: if(pressedKeys.Contains(Keys.N) && !previousKeys.Contains(Keys.N)) { //Do something if N was just pressed; previousKeys = pressedKeys; }

Explanation

The previousKeys Array is used to enable "Toggle-Keys". Toggle-Keys are keys that only run their action once even if you hold them for a longer time

Clone this wiki locally