Holding right key down

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

guyash2
Junior Coder
Posts: 40
Joined: Thu May 05, 2005 2:45 pm

Holding right key down

Post by guyash2 » Fri Jun 10, 2005 7:04 am

Hi,
I would like to hold the right key down for a few seconds but when I record it, it writes "press right *xxx" (xxx=number of times). So how can I hold it down consecutivenly? and not for number of times.

thanks.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Jun 10, 2005 1:35 pm

The right arrow key is "strobed" at a frequency defined by your keyboard. Macro Scheduler cannot control that. Not all keys are strobed. For example the Shift/Ctrl/ALT/CAPS LOCK, etc. keys are not. That is why those keys can have a PRESS and RELEASE function. They only send a signal when the strobe sees that they have changed state.

If you want to press right arrow for x seconds, then record macro holding down right arrow for 1-3 seconds, timing it. Edit recorded macro to see how many Press Right*xxx you get and calculate how many you get per second. Now convert the time you want to hold down into seconds, then convert that into nnn and make a loop that increments with a single Press Right command.

Code: Select all

//For Example: if keyboard sends 300 strobes per second
//Want to hold down for 5 seconds
//Count will be 300 * 5 = 1500

Let>Count=1500
Let>k=0

Label>Loop
k=k+1
Press Right
If %k%<%Count%,Loop,End
Note that strobe rate will vary from computer to computer and from keyboard to keyboard on the same computer. Strobe rate is variable on the computer. Start/Settings/Control Panel/Keyboard. Strobe rates are relative, not absolute values. Settings changed here will affect all your other programs.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

guyash2
Junior Coder
Posts: 40
Joined: Thu May 05, 2005 2:45 pm

Ok..

Post by guyash2 » Sat Jun 11, 2005 4:26 pm

I understand now.
Thank you very much:)

Gnord
Newbie
Posts: 3
Joined: Sun Jun 12, 2005 3:42 am

Post by Gnord » Sun Jun 12, 2005 3:49 am

Are you sure, because I disagree, I can write software that detects the initial moment a keyboard button is pressed, and released, and that is not always strobed. This goes for all buttons, letters, arrows, etc.

I'd have to go test it, but I'm fairly sure it is possible to detect when a button is pressed, and released, as opposed to the strobing idea you are saying. My intuition says that if I do a looping method, then I'm going to give some of my more sophisticated programs a heart attack when I send a ton of button presses into it.

EDIT: My program cannot detect one hundred consectuive "press right" commands; this particular program has a bit of lag when trying to detect an arrow press, IE I have to hold it for slightly longer than you would normally expect in order to get a result - It is somebody else's program, I can't change that lag. I seriously need a method of HOLDING the button for about one second. I know for sure in java programing that I can detect when a button is pressed and/or released, and that it is very different from the strobing idea you are speaking of. Also, most games work that way by NECCESSITY, because of the way a game must move smoothly. I will try to come up with an example that will demonstrate that 100 "press right" is 100 separate keypresses, as opposed to one long hold of the keypress.

EDIT2: I have an example for you:

Test.htm:

Code: Select all

<html>
<head>
	<script language="javascript">
		function KDown(e){
			if (!e){e = window.event;}
			var buttonKeyCode = e['keyCode'];
			KDownRecord=KDownRecord+buttonKeyCode+"<br>";
			numKDown++;
			DisplayResult();
			return false;
		}
		
		function KPress(e){
			if (!e){e = window.event;}
			var buttonKeyCode = e['keyCode'];
			KPressRecord=KPressRecord+buttonKeyCode+"<br>";
			numKPress++;
			DisplayResult();
			return false;
		}

		function KUp(e){
			if (!e){e = window.event;}
			var buttonKeyCode = e['keyCode'];
			KUpRecord=KUpRecord+buttonKeyCode+"<br>";
			numKUp++;
			DisplayResult();
			return false;
		}
		
		document.onkeydown      = KDown;
		document.onkeypress     = KPress;
		document.onkeyup        = KUp;

		var numKPress=0;
		var numKDown=0;
		var numKUp=0;
		
		var KDownRecord="Key Down Record<br>";
		var KPressRecord="Key Press Record<br>";
		var KUpRecord="Key Up Record<br>";
		
		function DisplayResult(){
			Results.innerHTML="Key Hits (up/down): " + numKPress + 
			                  "<br>Key Presses (down only): " + numKDown + 
			                  "<br>Key Releases (up only): " + numKUp;
			DownRecord.innerHTML=KDownRecord;
			PressRecord.innerHTML=KPressRecord;
			UpRecord.innerHTML=KUpRecord;
		}
	</script>
</head>
<body>
	<span id="Results"></span><br><br>
	<table><tr>
		<td><div id="DownRecord"></div></td><td><div id="PressRecord"></div></td><td><div id="UpRecord"></div></td>
	<tr></table>
	<noscript>This uses JavaScript, you need to turn it on</noscript>
</body>
</html>
Macro:

Code: Select all

press left
press left
press left
press left
press left
Method:
Save the code above to a text document named Test.htm.
Create a macro with a bunch of "press left" 's in the macro. From there, open the webpage, and press and hold any normal arrow or letter button, for example left arrow. The browser knows that there has not been any button released yet. Hotkey and execute the macro into the browser, and the release counter goes nuts. The button is being pressed and released, where under optimal circumstances, the browser should know that a button is pressed, and NOT released.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Jun 12, 2005 5:44 am

I am trying to provide a solution using Macro Scheduler functions.
I am confused about the javascript info.
How does this allow Macro Scheduler to hold down the right key for x seconds?
--------------------------------------------

What is the purpose to hold RIGHT down for x seconds, if not to continuously send "RIGHT ARROW" commands? Why does guyash2 want to do this?

It makes no sense to press it down and hold it with no strobes and then release it. If that is the case, then why not just press and release immediately?

If you want to trigger an event x seconds after pressing Right....if so, then just do Press Right, Wait>x, Next Command.

---------------------------------------
I will try to come up with an example that will demonstrate that 100 "press right" is 100 separate keypresses, as opposed to one long hold of the keypress.
I agree that Press Right *100 is the same as pressing the right arrow 100 times. The original question is how to hold the key down for a fixed number of seconds.[/quote]

The javascript code that notices when a key is released is responding to the KeyUp/Down signal that is sent in addition to the strobe signal that sends the value of the active key(s).
-------------------------------------
Re problem with other programs, it is really necessary to do SetFocus>WindowName* before using a Press command in Macro Scheduler.

So the string of RIGHT commands is something that is expected in this window anyway or it would not be asked for. I don't see the problem, unless someone is using the keyboard while the macro is running and changes the active window that has the focus.
---------------------------------------------------
Is Gnord the same person as guyash2?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Gnord
Newbie
Posts: 3
Joined: Sun Jun 12, 2005 3:42 am

Post by Gnord » Sun Jun 12, 2005 8:19 am

No, not the same person.

I have a similar problem that is not solved by the repeatedly pressing a button method.

I think your main question is: why?

I stated earlier that the program I am using has a momentary lag between the button is actually pressed, and when it is actually registered.

If you know code, I think I can explain it this way: The program I am using and have mentioned is probably is coded such that (note I say probably) the program is constantly testing the status of the button, whether it is pressed or not pressed. Unfortunately, the program has a lot to do, so it's loop is not being executed fast enough to detect the way macro scheduler presses a button. It does not respond immediately when a button is pressed, rather it responds as soon as it detects that the button's status is pressed, vs. unpressed. So, macro scheduler says "press and release the button." According to my program, it tests the button, the button is pressed and released before it can test the button again. I will note that this is not a program that natively runs in windows, which is why it does not react immediately when the button is pressed, as a normal windows program will.

Anyway, as I was saying before, the HTML demonstrates that macro scheduler is NOT capable of pressing and holding an arrow key, without activating the key-up function. When I open the html document, and press and hold the arrow key, I am accumulating kdown events, but ZERO kup events. Macro scheduler is not capable of this, it cannot press an arrow or text character without generating a key-up event.

Or, at least as far as I know. I would like to propose that all buttons, every single last one, have three functions, instead of the current, one: a KeyDown function, a KeyUp function, and a KeyPress function.

the KeyDown command should essentially presses a keyboard button down indefinitely until a KeyUp command is sent.
the KeyUp command should release a button that has been toggled down by KeyDown.
the KeyPress command should do what we currently know as the "press" command, which simply presses and immediately releases the button.

If this is already possible, I'm simply searching for a way to HOLD a button, arrow or letter, so that no "keyUp" event gets registered in the html page I have given as an example.

As a further example, I have already stated that games, even games coded natively in windows, do not detect repetative keystrokes when a button is pressed and held. Rather, it detects the initial keydown event, and reacts as the button is held until it recieves a keyup event. So, the strobing method you describe does not hold true for a large significant number of programs. The game registers an even smooth hold of the button, where the strobing method you describe registers a Press, pause, presspresspresspresspress... Games have no such restriction.

As I have said, in Java programing you again do not register the strobing effect, you register the keydown and keyup events. DirectX programs also do this, as well as any program which requires SMOOTH movement when a particular button is pressed, rather than the jerky movement done by press, pause, presspresspress, strobing method you describe.

[edit]PS: It's not a window focus issue.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sun Jun 12, 2005 8:55 am

Hi,

You are correct that the native keyboard functions in Macro Scheduler press and release the key immediately. In otherwords they issue a standard keypress. For more control you can use the Windows API function keybd_event. Here's an example which presses the down arrow and waits 10 seconds before releasing it:

//Key codes at http://www.mjtnet.com/vkcodes.htm
Let>VK_DOWN=40
Let>ExtendedKey=1
Let>KeyUp=2

//Press Down Arrow
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,ExtendedKey,0

//Wait 10 seconds
Wait>10

//Now release Down Arrow
Let>FLAGS={%ExtendedKey% OR %KeyUp%}
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,FLAGS,0

The down arrow is an extended key hence the ExtendedKey flag being applied. This code could be applied to any virtual key code. See the link in the comment for a list of virtual key codes.
MJT Net Support
[email protected]

Gnord
Newbie
Posts: 3
Joined: Sun Jun 12, 2005 3:42 am

Post by Gnord » Sun Jun 12, 2005 9:52 am

That works wonderfully in the webpage, yet still does not have the desired effect in my program. :(

For my next example, I will use notepad.

open notepad, and put in the following:

Code: Select all

test
test
test
test
test
... etc
for about twenty lines.

click on the top line, so the cursor is blinking at the top, and activate the following macro:

Code: Select all

SetFocus>Untitled - Notepad

//Key codes at http://www.mjtnet.com/vkcodes.htm
Let>VK_LEFT=37
Let>VK_UP=38
Let>VK_RIGHT=39
Let>VK_DOWN=40

Let>ExtendedKey=1
Let>KeyUp=2

//Press Down Arrow
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,ExtendedKey,0

//Wait 10 seconds
Wait>3

//Now release Down Arrow
Let>FLAGS={%ExtendedKey% OR %KeyUp%}
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,FLAGS,0
(I put the other four arrow keys in there because once this works, I hope to use all four.)

What you expect is notepad to quickly scroll down the list, until the cursor hits the bottom. Unfortunately, it moves only once.

In my program (FCEU in case you're wondering http://www.emulator-zone.com/doc.php/nes/fceultra.html) still has no response at all. :(

Thank you though, support for that piece of info. It doesn't help me for what I'm trying to do right now, but I'm sure that will be useful later. Do you know of any other possible method to hold the down arrow? I am not sure, but it may be recognizing the button via an extended ascii code. Is there any way to hold one of those down?

Your help is greatly appreciated.

[edit]
I have improved information for you.
The program I use (FCEU) does not recognize "press np2", or any repeated number of those commands.
It does not recognize the virtual keycodes, via the user32.dll example you have given me, BUT
It does recognize the "send>s" command, not every time it is sent, but occationally, like a small fraction of those commands are recieved and processed by the program. So, something in the send command lasts long enough for the program to detect, where as the other two methods either don't work, or are too fast to be detected, which I am not sure, because I haven't analyzed the source on FCEU.

I hope that could possibly help give you a better grasp, and hopefully help you to give me a solution. I'd like to note, the "send" function registers SOMETIMES, not totally how I would like it to, as if I were HOLDING the S key for example.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sun Jun 12, 2005 10:54 am

Gnord wrote:What you expect is notepad to quickly scroll down the list, until the cursor hits the bottom. Unfortunately, it moves only once.
No that is not what I would expect. Only one key down has been issued. So I would expect to see the cursor move only one line down.

My comments in that example are misleading. The comment says "press key down for 10 seconds". Actually that's not the case - the script presses key down once, waits 10 seconds (everthing halts for 10 seconds) and then the key is released. So actually that's only one distinct key event.

In contrast the following script HOLDS the key down for 10 seconds:

VBSTART
VBEND

//Key codes at http://www.mjtnet.com/vkcodes.htm
Let>VK_DOWN=40
Let>ExtendedKey=1
Let>KeyUp=2

//focus notepad - put a few lines in notepad and set cursor to first line
SetFocus>Notepad*

//Press Down Arrow for 10 seconds
VBEval>Timer,stTime
Let>StopTime=stTime+10
Label>press_loop
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,ExtendedKey,0
VBEval>Timer,elapsed
If>elapsedFLAGS={%ExtendedKey% OR %KeyUp%}
LibFunc>user32.dll,keybd_event,r,VK_DOWN,0,FLAGS,0
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Jun 12, 2005 5:03 pm

Thanks Support. I thought that perhaps we have gone beyond the "Beginners" classification of this forum. But I guess there are different groups of beginners. Using the Windows API calls is a new beginning for me.

Wondering if you can provide a link to some good sources that will open the doors to LibFunc> for me? Need to relate the Macro Scheduler syntax to the documentation. that I can locate for the dlls.

In the sample above you are using LibFunc without using LibLoad and LibFree. When and why is this acceptable? Macro Scheduler HELP seems to imply the need to surround LibFunc calls. Hmmm...Ok, I think I see. LibFunc can call the function directly OR the handle defined with LibLoad. Either is acceptable as the first parameter.

Thanks (I think) for encouragming me to dig deeper into this recent capability that I have been ignoring. I guess the only way to do this is to start experimenting with it. I see that LibFunc is not one more single function, but is a whole new drawer full of tools to be added to my toolbox.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jun 13, 2005 2:29 pm

Try:

http://www.dependencywalker.com/

This free util lets you view the inner workings of a DLL. After you find what might appear to be the function you need, click on it and the appropriate MSDN web page will be opened for you providing the details of how to use it.

Good luck,
Dick

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Mon Jun 13, 2005 2:40 pm

This site is also quite good, with a list of API functions and examples in VB. While the examples are in VB it is fairly easy to see how these functions can be used with LibFunc:

http://www.mentalis.org/apilist/apilist.php
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Jun 13, 2005 3:33 pm

Thanks for the references.

I already found the link to mentalist.org shortly after I send earlier message.

And I will download dependencywalker for testing also.

Appreciate your help........
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

sirjober
Newbie
Posts: 3
Joined: Fri Feb 03, 2006 11:23 am

Post by sirjober » Sat Feb 04, 2006 12:04 am

:!: :shock: :?:

It is all very interesting the many ways that you can simulate a keypress, but it isn't working for me either.

I am trying to control the movement of my character in a game called World of Earcraft, I have tried many methods to try making my character turn a full 180 degrees but unsuccessfuly.

The 'loop x,x=x+1 untill x=number', simply crashes the program. I tried inserting a 'wait>0.1' in each loop to prevent flooding and it "works" in a sense that the character turns pixel per pixel for a LONG period of time; which isn't what i'm trying to accomplish.

I'm also a beginner in programming so I cannot apply the method stated by Support, it simply does nothing to the program.

Any suggestions? :?

idiot
Macro Veteran
Posts: 152
Joined: Thu Mar 01, 2007 9:21 am

need help with arrows

Post by idiot » Thu Jul 05, 2007 12:42 pm

i've tried this code for ctrl and it works but when i try to use same method for arrow keys it dosent i tried the examples you showed for arrow keys but they dont seem to work i dont seem to have the ability to press and release arrows im using windows 2000 on thinkpad 600e
when it sends the command to press down it uses my number keys on my labtop as if they where in numlock mode how can i get macro scheduler to use arrow keys on my labtop?

Code: Select all

setfocus>endless online
Let>VK_control=17
Let>ExtendedKey=1
Let>KeyUp=2

Press ctrl
LibFunc>user32.dll,keybd_event,r,VK_control,0,ExtendedKey,0
Wait>10
Release ctrl
Let>FLAGS={%ExtendedKey% OR %KeyUp%}
LibFunc>user32.dll,keybd_event,r,VK_control,0,FLAGS,0
if idiots rule the world then im the king!!!!
i want a free t-shirt give me all of your rep!!!
please give me pro version of macro scheduler and appnavigator!!!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts