Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


check for keypress once

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Mon Jul 19, 2021 8:19 pm    Post subject: check for keypress once Reply with quote

how do i check for keypress once? so if the user holds a certain key down, they have to press the key again to run a function again.
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Mon Jul 19, 2021 9:49 pm    Post subject: Reply with quote

Code:
if form then form.destroy() form = nil end
form = createForm()
form.Position = poDesktopCenter

local b1 = createButton(form)
b1.Left=115 b1.Top=30 b1.Width=90 b1.caption="Click me!"

local clicked=0
b1.OnClick=function()
clicked=tonumber(clicked) + 1
  if clicked==2 then
  showMessage("You got me!")
  clicked=0
  else
  showMessage("Keep searching. ;)")
  end
end
Back to top
View user's profile Send private message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Mon Jul 19, 2021 10:06 pm    Post subject: Reply with quote

i dont wanna sound rude but i think i should explain it abit more, im trying to check if a user pressed a key on the keyboard e.g Q for example. i've tried using timers to check isKeyPressed and hotkeys but if i add + 1 to some variable, it will keep spamming + 1 to the variable unless the user stops pressing the key.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Mon Jul 19, 2021 10:23 pm    Post subject: Reply with quote

Create a hotkey that checks a guard variable. If it's unset, then set it, create a timer to unset it when the key isn't pressed anymore (and destroy the timer), and do whatever else you want.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Mon Jul 19, 2021 11:12 pm    Post subject: Reply with quote

i think i got it:
Code:
a = 0
ta=createTimer()
ta.Interval=1
ta.OnTimer=function(ta)
    if isKeyPressed(VK_A) then
        a = a + 1
    else
        a = 0
    end
    if a == 1 then
        print('Pressed A!')
        ta.destroy()
    end
end


thanks
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Tue Jul 20, 2021 12:34 am    Post subject: Reply with quote

It turns out that this is a keyboard key!
You can simply be presented with different options and you get the idea that helps you.
Here is an option:

Code:
if htkey then htkey.Destroy() htkey=nil end
if htkeyTimer then htkeyTimer.Destroy() htkeyTimer=nil end
htkeyTimer=createTimer() htkeyTimer.Interval=10 htkeyTimer.Enabled=true

function clickKey()
print("Hello User")
end

htkeyTimer.OnTimer=function()
if isKeyPressed(VK_F8) then
htkeyTimer.Enabled=false
local answer = messageDialog("Do you confirm to turn on the function?", mtWarning, mbYes, mbNo)
 if answer == mrYes then
 htkey=createHotkey(clickKey, VK_F9)
 showMessage("Press the 'F9' key to run the function.");
 else
 if htkey then htkey.Destroy() htkey=nil end
 htkeyTimer.Enabled=true
 end
end
end
Back to top
View user's profile Send private message
WhiteSnoop
How do I cheat?
Reputation: 1

Joined: 15 May 2009
Posts: 7
Location: http://alino.ucoz.com

PostPosted: Thu Jul 22, 2021 12:16 pm    Post subject: Reply with quote

I figured I'd share my way of tackling the issue. Keep in mind I very much optimized it into a function because I needed a table of hotkeys to process

Code:
function hotkeyDebug(tbl)
  for i, entry in pairs(tbl) do
    local key = isKeyPressed(entry.hotkey)
    if not entry.isHolding and key then
      entry.func()
      entry.isHolding = true
    elseif not key then
      entry.isHolding = false
    end
  end
end


Then I have this in my actual mission script
Code:
local M1_debugKeys = {
  {
    hotkey = VK_F11,
    func = function()
      setGameStatus(1)
    end,
    isHolding = false,
  },
  {
    hotkey = VK_NUMPAD0,
    func = function()
      teleportMainPlayerToWaypoint(419)
    end,
    isHolding = false,
  },
}
hotkeyDebug(M1_debugKeys)
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites