onkeydown

Post » Sat May 28, 2011 5:26 pm

When I am writing scripts in the CS, opened with the OBSE launcher, I can use OBSE functions just fine, except it will not let me use the onkeydown. Anything i should know about the function? any reason it wouldn't work? basically i just need to know exactly how it works.
User avatar
Kara Payne
 
Posts: 3415
Joined: Thu Oct 26, 2006 12:47 am

Post » Sat May 28, 2011 6:57 am

When I am writing scripts in the CS, opened with the OBSE launcher, I can use OBSE functions just fine, except it will not let me use the onkeydown. Anything i should know about the function? any reason it wouldn't work? basically i just need to know exactly how it works.

Nothing special about OnKeyDown - I use it several places in my mods. Here's an example from Map Marker Overhaul, where you can place a player-added map marker by pressing a key (M by default):

	if OnKeyDown tnoMMO.markerKey               ...	endif


Where tnoMMO.markerKey is just a short variable that by default is set to 50 (M). So if you want to check for the press of an N, you can write "if OnKeyDown 49".
User avatar
Matthew Aaron Evans
 
Posts: 3361
Joined: Wed Jul 25, 2007 2:59 am

Post » Sat May 28, 2011 1:22 pm

I'm confused as to how this differs from IsKeyPressed. It looks like it's more transient and only returns true during a single frame when the hit is first hit, so you don't need your own variable to record whether you processed it, but it also looks like you could easily miss the event.The name makes it look like a new block type, so I wonder if it was originally intended to be one, but it couldn't be done that way.

When would you use each one?
User avatar
Amber Ably
 
Posts: 3372
Joined: Wed Aug 29, 2007 4:39 pm

Post » Sat May 28, 2011 12:45 pm

I'm confused as to how this differs from IsKeyPressed. It looks like it's more transient and only returns true during a single frame when the hit is first hit, so you don't need your own variable to record whether you processed it, but it also looks like you could easily miss the event.The name makes it look like a new block type, so I wonder if it was originally intended to be one, but it couldn't be done that way.

When would you use each one?

I use OnKeyDown and OnControlDown a lot, and almost never IsKeypressed. You will want to use the former in every situation when you want something to happen due to the player pressing a key. The documentation is a bit misleading: The OnKeyDown returns true the first frame it is called after a key is pressed, so if your script only runs once every 10th frame, you will still not miss it.

if OnKeyDown 50  doSomethingendif


is the equivalent to

short key_down...if IsKeyPressed 50  if key_down == 0    set key_down to 1    doSomething endifelse  set key_down to 0endif


As you see, the former is much simpler and clearer.
User avatar
Scott Clemmons
 
Posts: 3333
Joined: Sun Sep 16, 2007 5:35 pm

Post » Sat May 28, 2011 2:42 pm

Another good example is to check whether Ctrl/Alt/Shift keys are pressed in conjunction with a given key.

Suppose you want different things to happen when the player presses the M key, depending on which secondary key is pressed together with the M key.

In this case, you don't want to react at the moment the auxiliary key is pressed (which would be the case with OnKeyDown), but you want to know if it is pressed at the time the M key is pressed.

if OnKeyDown 50   ;  M	if IsKeyPressed3        29     ;  Left Control		; . . .	elseif IsKeyPressed3    157 	;  Right Control		; . . .	elseif IsKeyPressed3     42 	;  Left Shift		; . . .	elseif IsKeyPressed3     54 	;  Right Shift		; . . .	elseif IsKeyPressed3     56 	;  Left Alt		; . . .	elseif IsKeyPressed3    184 	;  Right Alt		; . . .	endif	endif

User avatar
Yvonne
 
Posts: 3577
Joined: Sat Sep 23, 2006 3:05 am


Return to IV - Oblivion