View unanswered posts | View active topics It is currently May 21st, '13, 07:13




Reply to topic  [ 3 posts ] 
 Toggling action 
Author Message

Joined: Oct 18th, '09, 03:27
Posts: 82
Post Toggling action
Well this little snippit is nice for spamming a loot key, which I find useful.
However the only problem I have is correctly toggling the "on off" switch of the program.

Num1 = on
Num2 = off

Theres something wrong in my logic, can anyone spot it?

Code:
local x = 1
function Start()
   local game_win = win.Find("Gamewindow", nil, 0,0, win.FIND_TILE_INSIDE)
   if (game_win == nil) then
      print("\n** GameWindow window not found! **")
   else
      -- The window needs to be into focus for a proper screen shot
      --win.SetState(game_win, win.SW_SHOW)
      time.Sleep(0.200) -- Some time for window to come in focus..
   end
   local stateGO = input.KeyGetState(97)
   if stateGO == 1 then
      x = 1
      Init()
   else
      Start()
      return 1
   end
end

function Init()
   while x == 1 do
      local stateNO = input.KeyGetState(98)
      if stateNO == 1 then
         print("\nstop")
         stateNO = 0
         x = 2
         time.Sleep(1)
         Start()
      else
         print("\nlooting")
         input.KeyPressRaw(50)
      end
      time.Sleep(0.01)
   end
end
Start()


Oct 19th, '09, 03:39
Profile
Site Moderator
User avatar

Joined: Aug 18th, '09, 03:32
Posts: 1201
Post Re: Toggling action
First there is a problem with "input.KeyGetState()" the first fix/change for the upcomming "2.01" release.
It's not always returning the right flags.
I'll send you a "test" version so you can get around the problem till the next release.

There is actually three states/flags of output.
The core of it is two Win API calls, "GetAsyncKeyState()" and "GetKeyState()".
It was necessary to combine two because of some problem (undocumented?) where GAKS doesn't
properly return the flags for some keys.

Form input lib doc:
Quote:
state = input.KeyGetState(keycode)
Get key state by virtual key code. 0 means key up, 1 key is down, 2 key was down since last call.
The 2 case is somewhat unpredictable, so it’s generally better to look at the return as being 0 or 1.

You probably need to just change turn your
Code:
if stateNO == 1
into "not equals zero" tests like:
Code:
if stateNO ~= 0 then


Note in essence here by using input.KeyGetState() we're basically doing global "hotkeys" semantically.
There is not way other then this to this kind of thing in MM yet.
I considered adding "registered" hotkeys which are normally "event driven" things.
Maybe a good way would be a gueue to be polled to get the last "hotkey" presses.

If you just want command/control keys from input in the MM console you can use "console.GetKey()".
This is buffered input so you won't miss any presses. Maybe you want gobal hotkeys, just saying..

I'm not sure of your code there.
You'll probably want to keep track of the last key down state and compare it to it's current state in your polling.

Maybe something like this:
Code:
-- Someplace local to the module, or globally
-- Auto looting switch
gbLootOn = false -- Or 'true' if it should be on by default..
gLastF3State = -1  -- To keep track of physical key state


-- Hotkey poller
function PollHotkeys()

   -- Watching the "F3" key
   local KeyState = 0
   if (input.KeyGetState(input.VK_F3) > 0) then KeyState = 1 end
   if (gLastF3State ~= KeyState) then
      -- On captured key press, toggle the true/false state of the flag
      if (KeyState > 0) then gbLootOn = not gbLootOn end
      gLastF3State = KeyState
   end

   -- More hot keys here..
end

By saving a "last" state of the keys of interest we can get clean "debounced" key inputs.

-- Then in your code for looting
if gbLootOn then
   DoAutoLootCycle()
end
...


Note in a lot of the examples, etc., I use sort of a "Hungarian notation".
See: http://en.wikipedia.org/wiki/Hungarian_notation
I sometimes prefix with 'g' to indicate "global", 'b' for "boolean" values, etc.


Oct 19th, '09, 06:04
Profile

Joined: Oct 18th, '09, 03:27
Posts: 82
Post Re: Toggling action
Hmm. I'm still going over the code as I don't understand it line by line completely. But I got the gist of it, and did get it functioning the way I wanted it to.
Thanks a lot :lol:


Oct 19th, '09, 18:26
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Based on design by STSoftware, modded by Sirmabus Copyright© 2009-2011