IsKeyPressed - Input: Difference between revisions

Jump to navigation Jump to search
m
Convert source tags to syntaxhighlight
imported>Egocarib
m (Convert source tags to syntaxhighlight)
 
Line 9: Line 9:


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


== Parameters ==
== Parameters ==
Line 19: Line 19:
== Examples ==
== Examples ==
*Standard use.
*Standard use.
<source lang="papyrus">Bool bIsHotkeyPressed = Input.IsKeyPressed(42) ; Is L-Shift pressed?</source>
<syntaxhighlight lang="papyrus">Bool bIsHotkeyPressed = Input.IsKeyPressed(42) ; Is L-Shift pressed?</syntaxhighlight>
*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
<syntaxhighlight lang="papyrus">Bool bIsHotkeyPressed ; = False
Int Property iHotkey = 184 Auto ; R-Alt by default
Int Property iHotkey = 184 Auto ; R-Alt by default


Line 38: Line 38:
EndIf
EndIf
RegisterForSingleUpdate(0.25)
RegisterForSingleUpdate(0.25)
EndEvent</source>
EndEvent</syntaxhighlight>
*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.
*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()
<syntaxhighlight lang="papyrus">Event OnUpdate()
If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey)
If bIsHotkeyPressed != Input.IsKeyPressed(iHotkey)
bIsHotkeyPressed = !bIsHotkeyPressed
bIsHotkeyPressed = !bIsHotkeyPressed
Line 48: Line 48:
EndIf
EndIf
RegisterForSingleUpdate(0.25)
RegisterForSingleUpdate(0.25)
EndEvent</source>
EndEvent</syntaxhighlight>


== Notes ==
== Notes ==

Navigation menu