Quantcast
Detecting any key press and so on run function - WoWInterface
Thread Tools Display Modes
09-10-15, 06:09 AM   #1
Nikita S. Doroshenko
A Cyclonian
 
Nikita S. Doroshenko's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 44
Detecting any key press and so on run function

I'm looking for a simple way to run my own function if player press any key (any mean really any that game can read, for example "Del", "F1-F12", "Tab" and so on... i think only "Fn" key is not readable by default.

First i tried to use this code:

Lua Code:
  1. function Test()
  2.     print("Test")
  3. end
  4.  
  5. WorldFrame:SetScript("OnKeyDown", Test); -- Or UIParent:SetScript("OnKeyUp/Down", Test);

But it freeze all input from my keyboard to game - other words, i can't move my character, every keypress print "Test" in chat, so i can control game only with mouse. Any solutions?

PS. I don't care about what key has been pressed, i just need run function Test() if any key was pressed.
  Reply With Quote
09-10-15, 07:21 AM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 429
You should definitely not use SetScript on frames that aren't your own, rather use HookScript, as it will append the script snippet with your code rather than replacing the original script function. Having that said, I don't think it's generally a good idea to hook shit onto the WorldFrame. You could just create your own invisible frame and have it propagate the keyboard input instead. Like this:

Lua Code:
  1. local f  = Test or CreateFrame("Frame", "Test", UIParent)
  2.  
  3. local function TestPrint(self, key)
  4.    print(self:GetName(), key)
  5. end
  6.  
  7. f:SetScript("OnKeyDown", TestPrint)
  8. f:SetPropagateKeyboardInput(true)

Also, get in the habit of localizing your functions.
__________________

Last edited by MunkDev : 09-10-15 at 07:24 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Detecting any key press and so on run function

Thread Tools
Display Modes

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