Message with random probability when pressing a button

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Message with random probability when pressing a button

07 Jan 2017, 10:30

Hey, i forgot to login the first time i made this thread so i dont know if im posting this twice. I cant see it in the forum so far.

Im completly new to AHK, so when i try to search with google i understand almost nothing because its too advanced.
I know how to edit, run and compile a script.

What i need is a script that, whenever i press TAB, it opens a Message Window with a random chance (for example every 1 in 800 times) with a certain text. 799 in 800 times nothing happens.

Even better would it be if the 799 times there is a message too, the problem is that i dont wanna have a standard windows message window opened those times. Is there a way to show a message without windows message that takes focus?

Can someone help me with that? I would really be thankful for a code that i just have to copy and edit later (i wanna change probabilty later and maybe the hotkey).

Thanks in advance!
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Message with random probability when pressing a button

07 Jan 2017, 10:57

Give it your best shot. These documents should let you figure out what you need to do:

Hotkey
Random
If and operators
else
(Optionally, Blocks for the if/else statements.)
MsgBox and Tooltip (as ways to display your message)
return ; signals the end of a hotkey

I recommend you use Random to generate a number between 1 and 800, then use an if statement to check if it equals a single number. (There's a 1/800 chance that it would.)

If you have read through those and pieced together a script that still doesn't seem to be working, can you share what you came up with and we can guide you in solving it? I think it'll be best for you to learn making something up from scratch in this case, which will then be easier for you to modify as you start to see how things work.
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 11:07

Now that i see it, a tooltip would probably be perfect for me for showing the message
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 11:19

Tab::

Random Num, 1, 3
if (Num = 1) {

Send % Num
}


This is what i have so far. i use 1-3 so i can check better if its working. How do i get it into a tooltop instead of sending?
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Message with random probability when pressing a button

07 Jan 2017, 11:35

by using ToolTip instead of send :)

you also forgot the important "return" at the end.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 11:58

Edit:

Tab::

Random Num, 1, 3

if (Num= 1) {

MsgBox, , Title, Text,
}
else {

ToolTip Message, , , 1
}
sleep 1000
ToolTip, , , , 1
return



Its perfect like this. I get a message box only when i hit the 1/3 (or 1 in 800 later).

Only 1 thing lefti cant figure out. How do i make it possible that multiple tooltips can view at once? I want them to dissapear automatically (it already does), but i cant get a new tooltip until the old one dissappeared. It probably has to do with WhichToolTip, but i dont know how to apply it. Can anyon please help me?
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 12:27

Nevermind. Its ok when the tooltip dissapears when i click the button again.

Actually this is really cool, exactly what i need. Thanks for your help.
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 12:38

Ok 1 more thing:

I have it like this now:

Tab::

Random Num, 1, 600

if (Num= 1) {

MsgBox, , this is a title!,blablabla,
}
else {

ToolTip blablablabla, , , 1
}


return


When i click tab a new tooltip opens. If i close the tooltip with a command (empty tooltip) after a certain time, i cant open a new tooltip before that time is over.
Is there a way to get both? Get a new one when i click tab again, but if i dont, it closes in lets say 20 seconds?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Message with random probability when pressing a button

07 Jan 2017, 15:20

Include this in your else block when a tooltip is shown: SetTimer, HideTip, -20000 ; milliseconds. Then add this to the bottom of your script (after the return you currently have):

Code: Select all

HideTip:
Tooltip
return
No parameters on the tooltip command will hide (or clear) the one that is shown. And I believe this should work for your purposes.

Also, if you wrap your code in

Code: Select all

 tags, that makes it a bit easier for us to see at a glance which parts of posts are code.
 
 (Edit: Thanks smarq8 below; I forgot negative values were a thing for some reason. I've since edited.)
Last edited by Exaskryz on 07 Jan 2017, 16:01, edited 1 time in total.
smarq8
Posts: 69
Joined: 16 Jan 2016, 00:33

Re: Message with random probability when pressing a button

07 Jan 2017, 15:30

You can also use SetTimer with negative value, it will execute specyfied label after for eg 20000ms once and you do not need disable timer later (SetTimer, HideTip, Off)

Code: Select all

Tab::
   Random Num, 1, 600
   if (Num=1) {
      MsgBox, , this is a title!,blablabla,
   }
   else {
      ToolTip, blablablabla ; (, , , 1) <--- in this case it is not required 
      SetTimer, HideTip, -20000 ; milliseconds
   }
return

HideTip:
   Tooltip
return
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

07 Jan 2017, 17:38

Would it be hard to make all this happen not when i click a button, but when certain windows pop up?
I wouild identify the windows by their title, but theyre multiple different ones (including some of the same words).
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Message with random probability when pressing a button

07 Jan 2017, 17:51

WinWaitActive (and SetTitleMatchMode) may be what you're looking for?

Instead of manual triggering a hotkey, you'd probably want a Loop with WinWaitActive/WinWaitNotActive to alternate when the window appears and goes away.

There is WinWait as an alternative to WinWaitActive.
Flizzy
Posts: 8
Joined: 07 Jan 2017, 10:18

Re: Message with random probability when pressing a button

08 Jan 2017, 06:20

When i use the HideTip line i get an error message "target label doesnt exist". Also i want more than 1 label to run at the same time, is that possible? Each one to dissappear after a certain time. I know that it has to do with WhichTooltip, but i dont know how to have them stack up continously.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Message with random probability when pressing a button

08 Jan 2017, 10:27

For the label not existing, did you copy the full code of smarq8's? There is actually a bit more below the "return" - the codebox is scrollable.

You can have more than 1 label running at a time. Depending on how you're implementing the multiple tooltips, you can just have HideTip1, HideTip2, HideTip3 and change what label the SetTimer points to when it loads up that number Tooltip.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 467 guests