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 


fixing my SOMETIMES perfect lua script?

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

Joined: 22 Jun 2015
Posts: 34

PostPosted: Wed Jan 26, 2022 1:04 pm    Post subject: fixing my SOMETIMES perfect lua script? Reply with quote

hello, i want to be able to use up and down arrow keys on my keyboard to emulate mouse wheel up and down. where pressing up arrow, also acts as a single mouse wheel up, and down arrow for single mouse wheel down.
i however do not want to lose my normal up/down arrow functionality.

what my script does, is by pressing up arrow, does a mouse wheel up, then normal up arrow behavior, and vice versa for down.

it works usually, but here and there it will bug out, and it'll keep pressing me down or up, or double press a direction, or weird up/down loops. and I'm not exactly sure why.

is anyone actually able to tell what might be causing this weird behavior, and how to fix it?

much appreciated, thanks!



[code]
if hk then hk.destroy() hk=nil end

hk=createHotkey(function()
if getForegroundProcess()==getOpenedProcessID() then
--do the mouse wheel up when UP arrow is pressed
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0)
--lift key just in case idk
keyDown(VK_UP)
-- wait until the up key is pressed
while not isKeyPressed(VK_UP) do
-- do nothing
end
end
end, VK_UP)


hk=createHotkey(function()
if getForegroundProcess()==getOpenedProcessID() then

--do the mouse wheel up when UP arrow is pressed
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -120, 0)
---lift key just in case idk
keyDown(VK_DOWN)
-- wait until the down key is pressed
while not isKeyPressed(VK_DOWN) do
-- do nothing
end
end
end, VK_DOWN)
[/code]
Back to top
View user's profile Send private message
Frouk
Master Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 489
Location: mov dword ptr [Ukraine]

PostPosted: Thu Jan 27, 2022 3:26 am    Post subject: Reply with quote

why
_________________
void(__cdecl *Haxing)(HWND hGameWindow)
Back to top
View user's profile Send private message
catfood
Cheater
Reputation: 0

Joined: 22 Jun 2015
Posts: 34

PostPosted: Thu Jan 27, 2022 8:45 am    Post subject: Reply with quote

[quote="Frouk"]why[/quote]

uhm LOL, why what?

why do I want it fixed? because working > non working.

why do I want this behavior? because you can scroll thru shop windows in my game with a mouse wheel, but laptop touchpad scrolling=/= mouse wheel.

why does the script work the way it does? idk you tell me LOL
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Thu Jan 27, 2022 9:50 am    Post subject: Reply with quote

catfood wrote:
Frouk wrote:
why


uhm LOL, why what?

why do I want it fixed? because working > non working.

why do I want this behavior? because you can scroll thru shop windows in my game with a mouse wheel, but laptop touchpad scrolling=/= mouse wheel.

why does the script work the way it does? idk you tell me LOL


You will need a global mouse hook. Intercept the messages and handle them how you see fit. Because it's pretty low-level stuff, if the solution provided by Dark Byte isn't suffice, I would say that it's perfectly valid to expect that you won't receive a solution to this problem just because of what creating a global mouse hook entails.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Jan 27, 2022 1:26 pm    Post subject: Reply with quote

I don't look at improperly formatted code in much detail, but you're using the same global variable (hk) for both hotkeys. The first one gets leaked after you assign the second one. i.e. calling destroy will only destroy the second, not the first
That busy wait loop seems bad as well

_________________
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
catfood
Cheater
Reputation: 0

Joined: 22 Jun 2015
Posts: 34

PostPosted: Sat Jan 29, 2022 11:12 am    Post subject: Reply with quote

[quote="ParkourPenguin"]I don't look at improperly formatted code in much detail, but you're using the same global variable (hk) for both hotkeys. The first one gets leaked after you assign the second one. i.e. calling destroy will only destroy the second, not the first
That busy wait loop seems bad as well[/quote]

I don't know why the forum shows it that way, as I put it in [code] brackets and in the editor it appears correctly with proper tabbing.

anyway, I renamed the second hk functions to see if I could get it to behave any differently, to no effect. =/
is it formatted good for you here?
thanks!
https://pastecode.io/s/15x2054e
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Sat Jan 29, 2022 12:31 pm    Post subject: Reply with quote

catfood wrote:
...

Code:

if hk then hk.destroy(); hk=nil end


Missing semi-colon for statements that are on the same line

Code:

hk = createHotkey(
  function()
    local fgProc = getForegroundProcess()
    local oProc = getOpenedProcessID()

    if fgProc == oProc then
      if isKeyPressed(VK_UP) then
        mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0)
      else
        return nil
      end
    end
  end, VK_UP
)


This checks the foreground process for equality, if so then check if the up arrow is pressed, if the condition
is met then send the mouse_event message.

Code:

hk2 = createHotkey(
  function()
    local fgProc = getForegroundProcess()
    local oProc = getOpenedProcessID()

    if fgProc == oProc then
      if isKeyPressed(VK_DOWN) then
        mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -120, 0)
      end
    end
  end, VK_DOWN
)
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Sat Jan 29, 2022 1:24 pm    Post subject: Reply with quote

There's a checkbox when posting that says "Disable BBCode in this post". You probably have that checked.

You don't need the indentation level on the calls to createHotkey.

The busy while loop is awful and should not exist.

What's with the comment about lifting keys when you call keyDown?

I'm pretty sure CE just uses getAsyncKeyState for hotkeys. Events won't be swallowed by CE, so the normal behaviour of pressed keys (i.e. up/down) shouldn't go away. You should be able to just send the mouse event.

_________________
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
catfood
Cheater
Reputation: 0

Joined: 22 Jun 2015
Posts: 34

PostPosted: Sat Jan 29, 2022 2:22 pm    Post subject: Reply with quote

[quote="LeFiXER"]

le-code

[/quote]


your name is spot on sir!
yes, after a week and a half of trying to fix this myself, it is now working flawlessly! you are the absolute man!!
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Sat Jan 29, 2022 2:28 pm    Post subject: Reply with quote

catfood wrote:

your name is spot on sir!
yes, after a week and a half of trying to fix this myself, it is now working flawlessly! you are the absolute man!!


You're welcome. I'm not a professional by any means so I don't know if this is the most efficient way to do it; however, it does work and that said I'm glad to have been a help.
Back to top
View user's profile Send private message
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