WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Detecting any key press and so on run function (https://www.wowinterface.com/forums/showthread.php?t=52723)

Nikita S. Doroshenko 09-10-15 06:09 AM

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.

MunkDev 09-10-15 07:21 AM

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.


All times are GMT -6. The time now is 09:01 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI