Need help moving the mouse cursor relative using a Logitech G910 - lua

I'm trying to get some bog standard code to work on my Logitech G910 keyboard. It's nothing much...mostly it is used to help edit a shopping list on a website. To help remove one item at a time I simply have it left click, replace the current number (single digit item) with a 0, move the mouse relative to the right, click, press the up key five times, and move it relative back to the left so that it's in position.
Now I am well aware that I could just as easily change the code to move the mouse somewhere on the screen, and that's all well and good, but since this is a learning opportunity for me, I might as well exploit it for all it's worth for latter use in the future. That said this is the headache I'm having...when the cursor is moved relative to it's location on the screen that's one value, but to move it back into position that's a different value, or in this case if it's moved 100 to the right, it has to be moved -65 to the left to be in the same position. This, of course, makes no sense what-so-ever.
To add to the problem, as if this wasn't bad enough on it's own, the cursor doesn't always end up in the same place when the code is ran. Sure...it will be in place on the first execution of the code, but on the second it's out of place, third it might be in place, and the fourth it might be out of place, etc. While this is all a headache, I can at least say this much: when it comes to using relative commands it sure is nice to have it, especially where using an arrow key won't place it in the exact position it needs to be in. Slightly off mark? No problem...just have it go up -2 on the y axis and it's all golden. :D
That aside, to make things a little easier, here is the snippet of code that I am using.
if (event == "G_PRESSED" and mkey == 1 and arg == 3) then
--Delete the value and replace with a 0 on site
PressAndReleaseMouseButton(1)
Sleep(100)
PressAndReleaseKey("backspace")
PressAndReleaseKey("0")
Sleep(100)
MoveMouseRelative(100, 0)
PressAndReleaseMouseButton(1)
Sleep(100)
PressAndReleaseKey("up")
PressAndReleaseKey("up")
PressAndReleaseKey("up")
PressAndReleaseKey("up")
PressAndReleaseKey("up")
Sleep(200)
MoveMouseRelative(-65, -2)
end

Related

Logitech LUA Script: MoveMouseRelative not working as expected

I am trying to write a script which will spin my character 180 degrees in game while holding lctrl and right clicking. This script works, but takes forever to spin around because of the sleep timer:
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsModifierPressed("lctrl")) then
for i = 0, 96 do
MoveMouseRelative (125,0)
Sleep (1)
end
end
end
If I increase MouseMoveRelative beyond 125, it starts to move the mouse in the wrong direction. No matter what value I use (I've tried lots of values between 100 and 12,000), it always moves the mouse a very small distance either left or right.
If I eliminate the Sleep function, the results are inconsistent. It usually rotates my character between 80-140 degrees. I suspect this is because MoveMouseRelative starts by getting the current mouse position and it requires a delay before the position it gets is accurate.
Any guidance as to why MouseMoveRelative doesn't work correctly for values above 125? Or any advice on how to quickly move the mouse 12,000 relative units on the x-axis instantaneously?
If I increase MouseMoveRelative beyond 125, it starts to move the mouse in the wrong direction.
The allowed range is -127...+127
I suspect this is because MoveMouseRelative starts by getting the current mouse position
No.
It does not ask the current mouse position.
It invokes SendInput() without flag MOUSEEVENTF_ABSOLUTE to simulate relative mouse movement.
how to quickly move the mouse 12,000 relative units on the x-axis instantaneously?
for i = 0, 96 do
MoveMouseRelative (125,0)
end
If I eliminate the Sleep function, the results are inconsistent. It usually rotates my character between 80-140 degrees.
Try more precise version of Sleep()
for i = 0, 96 do
MoveMouseRelative (125,0)
FastSleep(1)
end
Or any advice on how to quickly move the mouse 12,000 relative units
on the x-axis instantaneously?
If you want to move relative 12000 instantaneously, why do you move 97 steps of 125 with 1ms delays?
97*125 is 12125
Why not MoveMouseRelative(12125, 0) ?
According to the manual MoveMouseRelative takes "a few milliseconds" to complete.
1 ms is not a few. Reading the mouse position too early will give you the starting position. So I would presume that spamming relative movements which refer to that position might cause problems.

Lua tables and screen coordinates. For every {x} at y do

I'm having a little curious sense of art in programming at the moment. And I want to script my Autotouch App on my iOS to generate Pixel Art inside of another app.
I was doing this previously by typing in code to tap at the screen at one coordinate, I did this 2000+ times and it got the job done. But there should be a better, smarter way to get it done.
My test image is going to be very symetrical to make things easy.
There is a code in the Lua app that I'm using to simply tap on the screen,
tap(x, y)
But I want to set this up like:
tap({xTable}, y)
But I'm not sure if that will "tap" at each x coordinate that I've listed for the y variable.
I want to paint a pixel at one very specific coordinate, and then step 5 pixels away and paint the next one, and repeat that until the end of the line.
Is this at all possible or am I reaching beyond the language capabilities?
Edit: for some reason my phone is not blocking code when I'm asking a question, if someone sees this and wants to edit, I would be grateful.
Is this at all possible or am I reaching beyond the language capabilities?
Not even close. I recommend you read Programming in Lua.
tap({xTable}, y)
But I'm not sure if that will "tap" at each x coordinate that I've listed for the y variable.
Why are you not sure? Did you not write it? If not, you can trivially write it yourself given tap:
function tapxs(xs, y)
for i,x in ipairs(xs) do
tap(x,y)
end
end
...
tapxs({10,20,30,40}, 10) -- tap at 10,10; 20,10; 30,10; etc.
I want to paint a pixel at one very specific coordinate, and then step 5 pixels away and paint the next one, and repeat that until the end of the line.
What is "the line"? Is it purely horizontal? You could write:
function tapHorizontally(startX, maxX, y, increment)
for x=startX,maxX,increment do
tap(x,y)
end
end
...
tapHorizontally(10,100,20,5) -- tap from 10,20 to 100,20 in 5 pixel increments
Of course, that's a bizarrely specific function. You'd typically write something that takes a starting x,y and ending x,y and draws between them, so you can support horizontal, vertical, diagonal lines all with the same function, but that requires more math.
The bottom line is: Lua is a full blown, powerful, high level programming language. It could be used to write the very app you're tapping on, or the app you're using to generate taps, so the limits are going to be your knowledge of programming/algorithms/math/etc.

How to move image with low values?

The problem is simple: I want to move (and later, be able to rotate) an image. For example, every time i press the right arrow on my keyboard, i want the image to move 0.12 pixels to the right, and every time i press the left arrow key, i want the image to move 0.12 pixels to the left.
Now, I have multiple solutions for this:
1) simply add the incremental value, i.e.:
image.x += 0.12;
this is of course assuming that we're going to the right.
2) i multiplicate the value of a single increment by the times i already went into this particular direction + 1, like this:
var result:Number = 0.12 * (numberOfTimesWentRight+1);
image.x = result;
Both of these approaches work but produce similiar, yet subtly different, results. If we add some kind of button component that simply resets the x and y coordinates of the image, you will see that with the first approach the numbers don't add up correctly.
it goes from .12, .24, .359999, .475 etc.
But with the second approach it works well. (It's pretty obvious as to why though, it seems like += operations with Numbers are not really precise).
Why not use the second approach then? Well, i want to rotate the image as well. This will work for the first attempt, but after that the image will jump around. Why? In the second approach we never took the original position of the image in account. So if the origin-point shifts a bit down or up because you rotated your image, and THEN you try to move the image again: it will move to the same position as if you hadn't rotated before.
Alright, to make this short:
How can i reliably move, scale and rotate images for 1/10 of a pixel?
Short answer: I don't know! You're fighting with floating point math!
Luckily, I have a workaround, if you don't mind.
You store the location (x and y) of the image in a separate variable... at a larger scale. Such as 100x. So 123.45 becomes 12345, and you then divide by 100 to set the attribute that flash uses to display.
Yes, there are limits to number sizes too, but if you're willing to accept some error rate, and the fact that you'll be limited to, I dunno, a million pixels in each direction, you can fit it in a regular int. The only rounding error you will encounter will be a single rounding error when you divide by 100 (or the factor you used). So instead of the compound rounding error which you described (0.12 * 4 = 0.475), you should see things like 0.47999999. Which doesn't matter because it's, well, so small.
To expand on #Pimgd answer a bit, you're probably hitting a floating point error (multiple +='s will exaggerate the error more than one *='s) - Numbers in Flash are 53-bit precision.
There's also another thing to keep in mind, which is probably playing a bigger role with such small movement values; Flash positions all objects using twips, which is roughly about 1/20th of a pixel, or 0.05, so all values are rounded to this. When you say image.x += 0.12, it's actually the equivalent of image.x += 0.10, hence which the different becomes apparent; you're losing 0.02 of a pixel with every move.
You should be able to get around it by moving to another scale, as #Pimgd says, or just storing your position separately - i.e. work from a property _x rather than image.x so you're not losing that precision everytime:
this._x += 0.12;
image.x = this._x;

Shifting elements in match3 kind of game

I am a beginner at ActionScript3, and for my learning purpose, I am trying to build a match3 kind of game. I am making is a clone of bejeweled kind of game. but instead of swapping, I have to delete those elements and shift the upper elements down and add new elements above those shifted elements.
I am able to delete matched elements and after matching I'm removing those elements but I am stuck with the shifting code. I am not able to shift those elements down.
I believe that you should take a look at Richard Lord's Tetris source code as your game may be somehow similar in mechanics to Tetris. Tetris shifts down rows when you get lucky.
Richard Lord is one of the Flash Gurus and his way of doing things may seem pretty advanced for a starter like you. Take a look over the source code and see if it fits you. What I can tell is that this is the proper way to make a game but maybe it's not the best point to start for a novice like you that is in urgent need.
http://www.richardlord.net/blog/actionscript-3-tetris-source-code
I would approach it like this in 2 parts.
Loop through your array of board positions starting from the bottom row and check for an empty unoccupied slot, if you find one do another loop through the row above till you find a piece on the same column. If you find a piece on the row above apply a tween to move it down to the empty slot and continue your loop.
Once you have looped through all rows, and animated all the tweens you need to loop through again to find all the gaps that need gems dropped into them. Create new elements at those positions and then move them up by (element height * row), apply another tween to animate them back to their starting position.

textmate move cursor without using the arrow keys

I'm in an ergonomic sort of mood, and realizing that I waste many precious milliseconds by lifting my hands up from the letter keys over to the arrow keys in order to move my cursor. Is there a handy alterate way to move the cursor I can access in Textmate?
Generally, I'm looking for ways to keep my head up more while programming. I'd like to, for example, be able to copy an entire line into the clipboard without lifting my fingers. Can textmate take me where I want to go, or should I start thinking about making a move to Vim or some such?
ctrl+f moves cursor one character forward
ctrl+b .. and backward
(hold alt/option to skip by word)
ctrl+a beginning of line
ctrl+e .. and end of line
ctrl+n move to next line
ctrl+p .. and to previous

Resources