Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Store Wildcard in a variable


  • Please log in to reply
5 replies to this topic
PTG
  • Members
  • 6 posts
  • Last active: Oct 30 2015 02:14 PM
  • Joined: 22 Dec 2014

Hello all! Thanks for taking the time to read this!

 

I'm an amateur programmer and I'm having a bit of trouble with autohotkey. I have a script that runs in the background, and when I enter certain 'modes' (like the capslock is on) it becomes Vim-like. Pressing "h" moves my cursor left, "l" moves my cursor right, etc.

 

I'm trying to implement another Vim-like feature — the 'f' key. The problem is, in Vim, if you were to type "fa" it would search to the right of the cursor until it hits the end of the line or finds the next "a" in the sentence. I understand algorithmically what to do, but I don't know how to store the variable when it could be anything.  how can I distinguish between the examples below?

 

fa

fb

fc

...

f!

[email protected]

f$

...

f0

f1

f2

...

etc

 

My other hotkeys work something like this:

h::
     if (CAPS_MODE == true)
          Send, {Left}
     else if (NUM_MODE == true)
          Send, !^s
     else
          Send, h
return

and I assume the one I'm trying to make needs to work something like this:

f::
    ;wait for next keypress
    ;store keypress in variable
    ;search remainder of sentence until variable matches or you hit end of sentence
return

Thanks for the help, I really appreciate the feedback!

-PTG



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

I'm assuming you want to move the cursor to the found character? Maybe something like this:

f::
	Input, SingleKey, L1
	ClipSaved := ClipboardAll
	Clipboard := "" ; Clear the clipboard. Required for ClipWait.
	Send, +{End}^c
	ClipWait, 0.1
	SendInput, % "{Left}{Right " Instr(Clipboard, SingleKey) "}"
	Clipboard := ClipSaved
return


PTG
  • Members
  • 6 posts
  • Last active: Oct 30 2015 02:14 PM
  • Joined: 22 Dec 2014

That works incredibly well, thanks! It seems to hang up, though. It's almost like it is timing out. It works for searching some thing flawlessly, and for others it will hang up and almost 'timeout' even if it does exist in the string.

 

I'm trying to figure out what your ClipWait, 0.1 is supposed to mean. I had thought it meant sleep for a tenth of a second, but why is the notation a decimal as opposed to in milliseconds.

 

You wouldn't have any idea why its timing out would you?

 

Thanks!



PTG
  • Members
  • 6 posts
  • Last active: Oct 30 2015 02:14 PM
  • Joined: 22 Dec 2014

Scratch that last bug. It works flawlessly, I just wasn't in the right context mode.

-PTG



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
@kon why have the clipwait timeout that Low when you don't handle the timeout event? (It will Continue as soon as the clipboard contains data anyway)

@PTG try and set up the clipwait to something like 5
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

PTG
  • Members
  • 6 posts
  • Last active: Oct 30 2015 02:14 PM
  • Joined: 22 Dec 2014

I swapped it to clipwait 5 and I don't notice much of a difference.

 

It seems silly to make another thread to ask for only a slight tweak on this, so I'll just ask it below...

 

This code doesn't seem to work....

f::
     if (CapslockMode == true)
     {
          If (A_PriorHotKey == "f" and A_TimeSincePriorHotkey < 500)
          {
              Send, f
          }
          else{
                Input, SingleKey, L1
                ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
                Clipboard := "" ; Clear the clipboard. Required for ClipWait.
                Send, +{End}^c
                ClipWait, 5 ;wait no longer than this to get clipboard contents
                SendInput, % "{Left}{Right " Instr(Clipboard, SingleKey)-1 "}"
                Clipboard := ClipSaved
          }
     }
     else
          Send, f
return

v:: 
if (CapslockMode == true) { 
     If (A_PriorHotKey == "f" and A_TimeSincePriorHotkey < 500) { 
          Send, v 
     } 
     else 
        EnableNumLockMode(false) 
} 
else 
     Send, v 
return

I'm trying to say "If I've just pressed the 'f' key and I immediately press 'f' again or 'v' then register it as 'ff' and 'fv', respectively. Am I going about implementing this all wrong? Does the code make any sense to anyone else?