I need a macro that starts when I press the F8 key and up

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
luca1974wow
Newbie
Posts: 15
Joined: Sat Dec 12, 2009 11:20 pm

I need a macro that starts when I press the F8 key and up

Post by luca1974wow » Thu Nov 11, 2010 3:07 pm

OnEvent>Key_Down,VK119,0,MainMacro
SRT>MainMacro

send>a

END>MainMacro
Label>IdleLoop
Wait>0.01
Goto>IdleLoop


That macro is good but work only if i press F8

I need a macro that starts when I press the F8 key and button and up key

(example the macro work when i press control F8)

Anyone can help me? please

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Nov 11, 2010 3:51 pm

So you want:

OnEvent>Key_Down,VK119,2,MainMacro

Note the bold parameter. The help topic for OnEvent says for this Key_Down parameter:
0: No modifier key
1: SHIFT key must also be pressed
2: CONTROL key must also be pressed
3: ALT key must also be pressed
4: SHIFT + ALT keys must be pressed
5: CONTROL + ALT keys must be pressed
6 SHIFT + CONTROL keys must be pressed
7: CONTROL + ALT + SHIFT keys must also be pressed
8: Windows key must also be pressed
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

luca1974wow
Newbie
Posts: 15
Joined: Sat Dec 12, 2009 11:20 pm

Post by luca1974wow » Thu Nov 11, 2010 8:54 pm

Sorry for my english


OnEvent>Key_Down,VK119,2,MainMacro
if Press F8+CONTROL the macro work



i need macro:

Press F8+UP and the macro work

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Nov 11, 2010 10:24 pm

Up is not a modifier key so there is no way to do that.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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

Post by JRL » Fri Nov 12, 2010 12:56 am

I like to think there's always a way. Sometimes its just not a provided way like a built in modifier key. By using a variable flag we can make any key a modifier key. While the value of the variable "F8KeyWasPressed" is zero, pressing up will only do whatever "Up" does. But wjen the F8 key is held down the value of "F8KeyWasPressed" is set to one. While it is one the subroutine tied to the Up key will perform whatever task you want the combination of F8+Up to perform.

Code: Select all

//F8=VK119
OnEvent>key_down,VK119,0,SetFlag
//Up=VK38
OnEvent>key_down,VK38,0,DoStuff
Let>F8KeyWasPressed=0

SRT>SetFlag
  Let>F8KeyWasPressed=1
END>SetFlag

SRT>DoStuff
  If>F8KeyWasPressed>0
    MDL>The key combination of F8 and Up were pressed
  EndIf
END>DoStuff


Label>Loop
  Wait>0.01
  If>F8KeyWasPressed>0
    Add>F8KeyWasPressed,1
    If>F8KeyWasPressed>10
      Let>F8KeyWasPressed=0
    EndIf
  EndIf
Goto>Loop

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Nov 12, 2010 1:22 am

JRL wrote:I like to think there's always a way.
Ditto - nicely done JRL... good concept.

On testing this, I find it also "fires" if you tap one then the other in fast succession... i.e. F8 and the Up Arrow key are not actually being pressed down simultaneously... but it still fires. Also, maybe 10% of the time, I pressed them both down more or less simultaneously and it did not fire at all... but on the next try it fired. This may be good enough for the poster's purposes though, good concept.

Since neither F8 nor the Up arror key can be a modifier... the only other technique I can think of might be... to keep the two OnEvents, one fires if F8 is down and one fires if Up Arrow key is down... and in the subroutine for each, don't check a flag, check directly (using a Win API call?) to see if the other key is also presently down and if so, perform the required function.

Just a thought, take care.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

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

Post by JRL » Fri Nov 12, 2010 4:26 am

On testing this, I find it also "fires" if you tap one then the other in fast succession... i.e. F8 and the Up Arrow key are not actually being pressed down simultaneously... but it still fires.
In the "Loop", there is a line that says:
If>F8KeyWasPressed>10
Change the 10 to a 2 and that issue will go away. What is happening is that there is a lag between when when the F8 key is released and the variable "F8KeyWasPressed" is set back to zero. During that lag, pressing the Up key will still pop up the modal message. Changing the ten to a two will reduce the lag to a more acceptable level.
Also, maybe 10% of the time, I pressed them both down more or less simultaneously and it did not fire at all...
With any real modifier key, pressing the two keys exactly simultaneously or pressing the main key prior to pressing the modifier key does not execute what ever the key combination is supposed to execute. That is also why I think that even if you used a Win API call as you described at the end of your post, you'd still need some sort of flag system to make one key a modifier of the other. Otherwise it wouldn't matter whether you pressed the up key first or the F8 key first.

I think the primary caveate to this is that usually a modifier actually modifies the activity performed by the main key being pressed. In this case the activity for the key is not actually altered. If there is an activity in the focused application that will be affected by the Up key, it will still be affected by the up key. This could be rectified by adding code similar to that found in THIS POST. By intercepting the keystroke(s) a script could be written that absolutely controls the actions of the keys.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Nov 12, 2010 7:01 am

JRL wrote:Changing the ten to a two will reduce the lag to a more acceptable level.
Yup, that definitely improves it.
Also, maybe 10% of the time, I pressed them both down more or less simultaneously and it did not fire at all...
JRL wrote:With any real modifier key, pressing the two keys exactly simultaneously or pressing the main key prior to pressing the modifier key does not execute what ever the key combination is supposed to execute. That is also why I think that even if you used a Win API call as you described at the end of your post, you'd still need some sort of flag system to make one key a modifier of the other. Otherwise it wouldn't matter whether you pressed the up key first or the F8 key first.
Yes and that's the point, with the change I suggested, it would not matter which key actually was pressed before the other... if either one is pressed, an OnEvent fires which will then check if "the other" key is also down and if it is... it has caught the fact that they are both down at the same time. I don't know at this point if there is a Win API call that can check that but if so, the concept seems valid.
JRL wrote:I think the primary caveate to this is that usually a modifier actually modifies the activity performed by the main key being pressed. In this case the activity for the key is not actually altered. If there is an activity in the focused application that will be affected by the Up key, it will still be affected by the up key. This could be rectified by adding code similar to that found in THIS POST. By intercepting the keystroke(s) a script could be written that absolutely controls the actions of the keys.
We don't know if the poster wanted either the F8 keypress or the Up keypress or both to be passed through to whatever application has focus at the time... or intercepted by this macro. If intercepted then yes, your technique at that post might be perfect.

Another caveat of the code you have provided... is that it has been written "as if" F8 was a modifier and Up was the key it was modifying... and by that I mean... I can press and hold down F8 and then very shortly after press and hold down Up and it always works... but if I switch the order around... i.e. hit the Up key first and then the F8 after, it absolutely never fires.

So, depending on the habit of the user and which key of those two they naturally hit first, if its F8, use your code as-is but if they normally hit the Up key first, they'd have to re-write it to switch around Up and F8 in your code.

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Nov 12, 2010 9:12 am

Ok, so we could use the GetKeyState function. The following will work when pressing F8+UP together:

Code: Select all

//UP=VK38
OnEvent>key_down,VK38,0,UpPressed
//F8=VK119
OnEvent>key_down,VK119,0,F8Pressed

Label>idle_loop
  Wait>0.2
Goto>idle_loop

SRT>UpPressed
  //Up was pressed, get VK_F8 keystate
  Let>VK_F8=119
  LibFunc>user32,GetKeyState,result,VK_F8
  If>result<0
    MessageModal>F8+UP was pressed
  Endif
END>UpPressed

SRT>F8Pressed
  //F8 was pressed, get VK_UP keystate
  Let>VK_UP=38
  LibFunc>user32,GetKeyState,result,VK_UP
  If>result<0
    MessageModal>F8+UP was pressed
  Endif
END>F8Pressed
Note two key down event handlers, one for UP and one for F8 the one for UP checks the keystate of the F8 key and the F8 handler chekcs the keystate of the UP key. The message is displayed if pressed. So this means we can detect both being pressed together. Insert a call to your subroutine in place of the MessageModal.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Nov 12, 2010 3:32 pm

Yes that's exactly it Marcus, thanks... and thanks JRL for the concept.

Take care all
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

luca1974wow
Newbie
Posts: 15
Joined: Sat Dec 12, 2009 11:20 pm

Post by luca1974wow » Sat Nov 13, 2010 7:43 am

thanks

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