Difference between revisions of "IsKeyPressed - Input"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>JustinOther
(Examples/Notes)
imported>Egocarib
 
(10 intermediate revisions by 3 users not shown)
Line 3: Line 3:
[[Category:SKSE]]
[[Category:SKSE]]
'''SKSE Member of:''' [[Input Script]]
'''SKSE Member of:''' [[Input Script]]
{{SKSEFunction|1.05.04}}


Checks if a given key is currently pressed. (This function requires SKSE)
Checks if a given key is currently pressed. (This function requires SKSE)


== Syntax ==
== Syntax ==
<source lang="papyrus">int Function IsKeyPressed(Int dxKeycode) global native</source>
<source lang="papyrus">Bool Function IsKeyPressed(Int dxKeycode) Global Native</source>


== Parameters ==
== Parameters ==
Line 20: Line 22:
*Do/Undo something when a key is pressed/released. [[States (Papyrus)|States]] ''could'' be used in lieu of the boolean guard, but would verifiably not save any resources in this context.
*Do/Undo something when a key is pressed/released. [[States (Papyrus)|States]] ''could'' be used in lieu of the boolean guard, but would verifiably not save any resources in this context.
<source lang="papyrus">Bool bIsHotkeyPressed ; = False
<source lang="papyrus">Bool bIsHotkeyPressed ; = False
Int iHotkey = 184 ; R-Alt
Int Property iHotkey = 184 Auto ; R-Alt by default


Event OnInit()
Event OnInit()
Line 27: Line 29:


Event OnUpdate()
Event OnUpdate()
If bIsHotkeyPressed != IsKeyPressed(iHotkey) ; Only run code when the status changes
If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey) ; Only run code when the status changes
bIsHotkeyPressed = !bIsHotkeyPressed ; Set bool to whatever it isn't
bIsHotkeyPressed = !bIsHotkeyPressed ; Set bool to whatever it isn't
If bIsHotkeyPressed ; == True
If bIsHotkeyPressed ; == True
Line 36: Line 38:
EndIf
EndIf
RegisterForSingleUpdate(0.25)
RegisterForSingleUpdate(0.25)
EndEvent</source>*Do something exactly once each time a key is pressed. We'll take the scaffolding from above as rendered given the difference is exclusively in the OnUpdate event. Without the primary check and consecutive bool-flip, [[Trace - Debug|Trace]] would be called every iteration.
EndEvent</source>
*Do something exactly once each time a key is pressed. We'll take the scaffolding from above as rendered given the difference is exclusively in the OnUpdate event. Without the primary check and consecutive bool-flip, [[Trace - Debug|Trace]] would be called every iteration.
<source lang="papyrus">Event OnUpdate()
<source lang="papyrus">Event OnUpdate()
If bIsHotkeyPressed != IsKeyPressed(iHotkey)
If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey)
bIsHotkeyPressed = !bIsHotkeyPressed
bIsHotkeyPressed = !bIsHotkeyPressed
If bIsHotkeyPressed
If bIsHotkeyPressed
Line 46: Line 49:
RegisterForSingleUpdate(0.25)
RegisterForSingleUpdate(0.25)
EndEvent</source>
EndEvent</source>
== Notes ==
== Notes ==
*To avoid multithreading mishaps, it's generally best to use [[RegisterForSingleUpdate - Form|RegisterForSingleUpdate]] rather than [[RegisterForUpdate - Form|RegisterForUpdate]].
*To avoid multithreading mishaps, it's generally best to use [[RegisterForSingleUpdate - Form|RegisterForSingleUpdate]] rather than [[RegisterForUpdate - Form|RegisterForUpdate]].
*If updating at tight intervals, optimization is key as it will make your polling script more responsive. It shouldn't be necessary to update at intervals < 0.25 and doing so just makes a script unnecessarily 'expensive'.
*If updating at tight intervals, optimization is key as it will make your polling script more responsive. It shouldn't be necessary to update at intervals < 0.25 and doing so just makes a script unnecessarily 'expensive'.
*When the [[Input_Script#Events|Input Events]] arrive, they'll be preferable to the above polling methods as their 'cost' will be less.
*Given the [[Form Script#SKSE Events|Input Events]] are available, they'll be preferable to the above polling methods for many applications as their 'cost' is less.
*Avoid using keys already claimed by Skyrim. Invariably, mod added hotkeys will conflict, so it's best to offer a way to rebind your hotkey(s) via [[GetNthKeyPressed - Input|GetNthKeyPressed]] from an [[Options Menu]] or other readily accessible means such as a scripted inventory item. This will afford your mod elasticity and allow it to work in tandem with other hotkey bearing mods.
*Avoid using keys already claimed by Skyrim. Invariably, mod added hotkeys will conflict, so it's best to offer a way to rebind your hotkey(s) via [[GetNthKeyPressed - Input|GetNthKeyPressed]] from an [[Options Menu]] or other readily accessible means such as a scripted inventory item. This will afford your mod elasticity and allow it to work in tandem with other hotkey bearing mods.
*Using a property to store the dxKeycode of the key you are polling will allow the same script to be easily reused to poll for different keys simultaneously, as this allows the dxKeycode to be set (or the default value overridden) in the Creation Kit without editing the script's source
== See Also ==
== See Also ==
*[[Input Script]]
*[[Input Script]]
*[[GetNumKeysPressed - Input]]
*[[GetNumKeysPressed - Input]]
*[[GetNthKeyPressed - Input]]
*[[GetNthKeyPressed - Input]]
*[[RegisterForKey - Form]]
*[[OnKeyDown - Form]]
*[[OnKeyUp - Form]]
*[[RegisterForControl - Form]]
*[[OnControlDown - Form]]
*[[OnControlUp - Form]]

Latest revision as of 19:04, 9 October 2014

SKSE Member of: Input Script

Minimum required SKSE Version: 1.05.04

Checks if a given key is currently pressed. (This function requires SKSE)

Syntax[edit | edit source]

Bool Function IsKeyPressed(Int dxKeycode) Global Native

Parameters[edit | edit source]

Return Value[edit | edit source]

Returns 'True' if the key is pressed and 'False' if not.

Examples[edit | edit source]

  • Standard use.
Bool bIsHotkeyPressed = Input.IsKeyPressed(42) ; Is L-Shift pressed?
  • Do/Undo something when a key is pressed/released. States could be used in lieu of the boolean guard, but would verifiably not save any resources in this context.
Bool bIsHotkeyPressed ; = False
Int Property iHotkey = 184 Auto ; R-Alt by default

Event OnInit()
	RegisterForSingleUpdate(0.25)
EndEvent

Event OnUpdate()
	If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey) ; Only run code when the status changes
		bIsHotkeyPressed = !bIsHotkeyPressed ; Set bool to whatever it isn't
		If bIsHotkeyPressed ; == True
			Debug.Trace("Hotkey Pressed")
		Else ; If bIsHotkeyPressed == False
			Debug.Trace("Hotkey Released")
		EndIf
	EndIf
	RegisterForSingleUpdate(0.25)
EndEvent
  • Do something exactly once each time a key is pressed. We'll take the scaffolding from above as rendered given the difference is exclusively in the OnUpdate event. Without the primary check and consecutive bool-flip, Trace would be called every iteration.
Event OnUpdate()
	If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey)
		bIsHotkeyPressed = !bIsHotkeyPressed
		If bIsHotkeyPressed
			Debug.Trace("Do something.")
		EndIf
	EndIf
	RegisterForSingleUpdate(0.25)
EndEvent

Notes[edit | edit source]

  • To avoid multithreading mishaps, it's generally best to use RegisterForSingleUpdate rather than RegisterForUpdate.
  • If updating at tight intervals, optimization is key as it will make your polling script more responsive. It shouldn't be necessary to update at intervals < 0.25 and doing so just makes a script unnecessarily 'expensive'.
  • Given the Input Events are available, they'll be preferable to the above polling methods for many applications as their 'cost' is less.
  • Avoid using keys already claimed by Skyrim. Invariably, mod added hotkeys will conflict, so it's best to offer a way to rebind your hotkey(s) via GetNthKeyPressed from an Options Menu or other readily accessible means such as a scripted inventory item. This will afford your mod elasticity and allow it to work in tandem with other hotkey bearing mods.
  • Using a property to store the dxKeycode of the key you are polling will allow the same script to be easily reused to poll for different keys simultaneously, as this allows the dxKeycode to be set (or the default value overridden) in the Creation Kit without editing the script's source

See Also[edit | edit source]