Thread Tools Display Modes
04/25/15, 02:18 PM   #1
Dimedius
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 3
Understanding Keypress events

I've been trying to understand how to use keypress events correctly. Basically I want to toggle between first person view and third person view (then add a condition that if in 3rd person to enable middle mouse/freelook). I don't really have any code written up, just doing some basic testing with CHAT_SYSTEM:AddMessage('string') to see if it runs.

Currently, from what I can tell, keypress events seem to only be used for when a window is created (like making a custom options window for your addon). Is there a way to call for keypress events without having to use a window? Or use an event that is constantly on so that a keypress event can be detected? Not sure how to word things as I am new to coding and addon making (I've been looking through the forums and wiki and feel like I the functions I try to use are incorrect).


lua

Code:
ZO_CreateStringId('SI_BINDING_NAME_TOGGLE_CAM', 'TOGGLE CAMERA')


local function ToggleCam()
	if key == KEY_V then
		CHAT_SYSTEM:AddMessage('Key press on V')
	end
end
xml

Code:
<Bindings>
	<Layer name='SI_KEYBINDINGS_LAYER_GENERAL'>
		<Category name='Camera'>
			<Action name='TOGGLE_CAM'>
				<Down>ToggleCam()</Down>
			</Action>
		</Category>
	</Layer>
</Bindings>
  Reply With Quote
04/25/15, 02:47 PM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,028
Keybindings can also be used without any window or even without any other control (label, texture, button, ...).

In your XML file just use

Code:
<Layer name="SI_KEYBINDINGS_CATEGORY_GENERAL">
Not SI_KEYBINDINGS_LAYER__GENERAL -> I'm not sure if this makes the difference but try it :-)

The keybinding will be usable inside the game's controls then and you designed it to be at "Camera" settings.
You will find it with the text that you have defined as 2nd parameter in function ZO_CreateStringId(..., 'TOGGLE CAMERA').
So you can use your key "V" here to assign it to this keybinding in the ESO controls.

If you press that key at the normal ESO ingame content (in your inventory, at the normal UI as you play and fight, etc.) the functionr egistered to this keybind will be executed.

But for your lua source code be carefull:
You defined a "local" function ToggleCam() but you have to define it global so the XML file will be able to use it too. XML file and lua source code are different files so they don't know each other if stuff is designed locally.
Try to remove the "local" in front of function and you should be able to call the function from an XML file, or by using it inside the chat by help of the /script command:

Code:
/script ToggleCam()
As global functions can be used outside your addon too I'd advice you to use more specific names for your addon, like "MyAddon_ToggleCam()" instead of "ToggleCam()" so you are sure no other function will exist in other addons or standard ESO source codes, that got the same name!

Edit:
About your function's content:
Lua Code:
  1. function ToggleCam()
  2. --key is an unknown variable here because you have not specified key inside the function parameters, like ToggleCam(key). So asking if content of key equals something will give you an lua error I assume as key is an unknown variable.
  3. --You don't have to ask for key here because the function will be called if you press the key that you have specified in the controls. It can be V or RETURN, or MOUSE1 or whatever. So just stipp the if key== KEY_V here
  4. --  if key == KEY_V then
  5.         CHAT_SYSTEM:AddMessage('[ToggleCam] Key was pressed')
  6. --  end
  7. end

Last edited by Baertram : 04/25/15 at 02:55 PM.
  Reply With Quote
04/25/15, 03:09 PM   #3
Dimedius
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 3
Originally Posted by Baertram View Post
Keybindings can also be used without any window or even without any other control (label, texture, button, ...).

In your XML file just use

Code:
<Layer name="SI_KEYBINDINGS_CATEGORY_GENERAL">
Not SI_KEYBINDINGS_LAYER__GENERAL -> I'm not sure if this makes the difference but try it :-)

The keybinding will be usable inside the game's controls then and you designed it to be at "Camera" settings.
You will find it with the text that you have defined as 2nd parameter in function ZO_CreateStringId(..., 'TOGGLE CAMERA').
So you can use your key "V" here to assign it to this keybinding in the ESO controls.

If you press that key at the normal ESO ingame content (in your inventory, at the normal UI as you play and fight, etc.) the functionr egistered to this keybind will be executed.

But for your lua source code be carefull:
You defined a "local" function ToggleCam() but you have to define it global so the XML file will be able to use it too. XML file and lua source code are different files so they don't know each other if stuff is designed locally.
Try to remove the "local" in front of function and you should be able to call the function from an XML file, or by using it inside the chat by help of the /script command:

Code:
/script ToggleCam()
As global functions can be used outside your addon too I'd advice you to use more specific names for your addon, like "MyAddon_ToggleCam()" instead of "ToggleCam()" so you are sure no other function will exist in other addons or standard ESO source codes, that got the same name!

Edit:
About your function's content:
Lua Code:
  1. function ToggleCam()
  2. --key is an unknown variable here because you have not specified key inside the function parameters, like ToggleCam(key). So asking if content of key equals something will give you an lua error I assume as key is an unknown variable.
  3. --You don't have to ask for key here because the function will be called if you press the key that you have specified in the controls. It can be V or RETURN, or MOUSE1 or whatever. So just stipp the if key== KEY_V here
  4. --  if key == KEY_V then
  5.         CHAT_SYSTEM:AddMessage('[ToggleCam] Key was pressed')
  6. --  end
  7. end

Thank you soooo much, this did the trick
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Understanding Keypress events


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off