AutoHotKey Help, matching character and replacing - autohotkey

My save feature, note I do not want to know better ways to save it or corrections to my save code, I just need help on the part where I need it to automatically remove additional non required/old brackets generated on save:
Here's the save button:
buttonSave:
FileAppend,
(
nprocess,exist, %Input%
nprocess,priority, %Input%, %PriorInput%
}
), C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
MsgBox, 0, Saved, %PriorInput% Priority to %Input% has been saved.
How can I get AutoHotkey to find } and remove any matching the char } and then add one } at the very end of the file, and have this looped?
I have a save feature and the output is this:
loop
{
process,exist, Steam.exe
process,priority, Steam.exe,
}
process,exist, Steam.exe
process,priority, Steam.exe,
}
I want it to find the end } and the middle } remove both of them and then add a single } at the very end. If you save, it always adds a bracket at the bottom.

buttonSave:
FileRead, Contents, C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
if not ErrorLevel ; Successfully loaded
{
StringTrimRight, Contents, Contents, 1 ; remove the end }
FileAppend, %Contents%, C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk
Contents = ; Free the memory
}
FileAppend,
(
process,exist, %Input%
process,priority, %Input%, %PriorInput%
}
), C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk
FileMove, C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk, C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk, 1 ; overwrite existing file
; Run, edit C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
MsgBox, 0, Saved, %PriorInput% Priority to %Input% has been saved.
; reload ; if Settings.ahk is included in this script
return

I'd use something like this (The loop causes high CPU utilization):
#NoEnv
#SingleInstance Force
#Persistent
Menu, Tray, Add
Menu, Tray, Add, Add Process, Add_Process
Menu, Tray, Default, Add Process ; doubleclick the tray icon to add a new process
SetTimer, set_priority, 500
return
set_priority:
Process,exist, Notepad.exe
Process,priority, Notepad.exe, L
; Add Process
return
Add_Process:
InputBox, ProcessName, ProcessName, Enter a Process.
if !ErrorLevel
InputBox, priority, priority, Enter a priority.
if !ErrorLevel
{
text =
(
Process,exist, %ProcessName%.exe
Process,priority, %ProcessName%.exe, %priority%
; Add Process
)
FileRead, Contents, %A_ScriptFullPath%
if not ErrorLevel ; Successfully loaded
{
StringReplace, Contents, Contents, `; Add Process, %text%
FileAppend, %Contents%, %A_ScriptDir%\Temp.ahk
Contents = ; Free the memory
}
FileMove, %A_ScriptDir%\Temp.ahk, %A_ScriptFullPath%, 1 ; overwrite existing file
reload
}
return

Related

How to edit paths in Windows clipboard?

When pressing "Ctr+C" while selecting some files in the explorer, their paths are saved in the clipboard and when later pressing "Ctr+V" the files are pasted. For an AutoHotkey script I need to edit these paths in the clipboard, so the explorer pastes other files in the location of the "Ctr+V" press. So for example I want to select the file "A.txt" in "C:\Folder1" with "Ctr+C", then activate the AutoHotkey script to change the stored information in the clipboard of "A.txt" to the information about another file "B.txt", so when pressing "Ctr+V" in "C:\Folder2" the explorer copies "B.txt" instead of "A.txt".
But I have no idea how the paths of "Ctr+C" are exactly stored in the clipboard and I found no useful documentation. Different programms to access the content of the clipboard give one of the three results: 1) [Absolute nothing] 2) "C://A.txt" 3) "file:///C:/A.txt". But when editing the clipboard to "C://B.txt" or "file:///C:/B.txt" I got no reaction of the file explorer when using "Ctr+V".
So how are the paths of copied files exactly stored in the clipboard and with which programm I could edit them?
Edit: Following expamle to explain exactly what my goal is: Suppose I have a file "A.txt" in path 1, which I want to move to path 2 and another file "B.txt" in path 3, which I want to move to path 4. The normal procedure would be:
1) Copy "A.txt" with "Ctr+C" in path 1.
2) Paste "A.txt" with "Ctr+V" in path 2.
3) Copy "B.txt" with "Ctr+C" in path 3.
4) Paste "B.txt" with "Ctr+V" in path 4.
The goal of my AutoHotkey script is to allow the following alternative:
1) Copy "A.txt" with "Ctr+C+1" in path 1.
2) Copy "B.txt" with "Ctr+C+2" in path 2.
3) Paste "A.txt" with "Ctr+V+1" in path 1.
4) Paste "B.txt" wiht "Ctr+V+2" in path 2.
At first glance it might seem unnecessary, but when organising a lot of files in more complex system, it would be really convenient for me. Ideally, it should also work when I add "C.txt", "D.txt", ... to be saved in the third/fourth/... clipboard slot and even when instead of "A.txt" I want to store multiple files in one clipboard slot; later I also want to implement "Ctr+X+[n]" to cut files.
I experimented a lot and was able to save the different paths of the files from the clipboard to AutoHotkey variables. But now I have to load the paths, which are stored in the AutoHotkey variables back to the clipboard, so the corresponding files are pasted with the explorer.
So just for an example: My AutoHotkey script detects the keyboard input "Ctr+C+1" while I select A.txt in the file explorer, then sends the computer the shorcut "Ctr+C" to store the path of A.txt in the clipboard and finally saves this path as "C:/A.txt" in the AutoHotkey varibale "clip1". Then I select B.txt in the file explorer, press "Ctr+C+2", so the AutoHotkey script presses "Ctr+C" to get the file paths into the Windows clipboard and then saves ít as "C:/B.txt" in "clip2". Now I press "Ctr+V+1", so the AutoHotkey script should write the path from the variable "clip1" back in the Windows, then press "Ctr+V" to paste the file in the explorer. But the really problem is now, that it is not enough to just edit the Windows clipboard to "C:/A.txt". Then so the explorer doesnt react to the input "Ctr+V", for which he should paste the file. So, the Windows clipboard has to be edited in another format, like for example "#explorer_file:///C:/A.txt" (just a complete stupid guess, since I have no idea how it looks). I need to find this format, so my AutoHotkey script can edit the currently saved paths to another paths.
I hope that explains all confusion about this confusing problem.
I'd use something like this:
#NoEnv
#SingleInstance Force
; Create a GUI with checkboxes to save the path of the last copied files and
; copy/move selected items to another directory opened in explorer:
OnClipboardChange("files_copied")
return
files_copied(type){
global
If (type == 1) ; the Clipboard contains text or files copied
{
; https://autohotkey.com/board/topic/150291-detect-clipboard-contents-as-text-file-etc/#entry735751
If (DllCall("IsClipboardFormatAvailable", "uint", 15)) ; the Clipboard contains files
{
ToolTip, file(s) copied
Gui, destroy
Gui, Color, ControlColor, Black
Gui, Font, CDefault, Lucida Console
Gui, Add, Button, x30 y5 w100 h26 gCopy ,Copy
Gui, Add, Button, x230 y5 w100 h26 gMove ,Move
Gui, Add, CheckBox, x20 y50 vCh200 gCheckAll cYellow, Select All
Gui, Add, CheckBox, x20 y75 vCh201 gUnCheckAll cYellow, De-Select All
Loop, Parse, Clipboard, `n, `r
{
y_pos := 100 + (A_Index * 25)
Gui, Add, CheckBox, x20 y%y_pos% vCh%A_Index% cYellow, %A_LoopField%
I := A_Index ; number of copied files
}
Gui_height := 125+I*25
Gui, Show, x100 y5 w400 h%Gui_height%, Files copied ; comment out this line if you want to only show the Gui after pressing F1
Sleep 1000
ToolTip
}
}
}
; Press F1 to show the Gui if it's hidden
F1:: Gui, Show
; or If the command in the above function is commented out
; F1:: Gui, Show, x100 y5 w400 h%Gui_height%, Files copied
CheckAll:
Loop, %I%
GuiControl,, Ch%A_Index%, 1
GuiControl,, Ch200, 1
GuiControl,, Ch201, 0
return
UnCheckAll:
Loop, %I%
GuiControl,, Ch%A_Index%, 0
GuiControl,, Ch200, 0
GuiControl,, Ch201, 1
return
Copy:
Gui, submit, nohide
If !WinExist("ahk_class CabinetWClass") ; explorer
{
MsgBox, No explorer window exists
return
}
WinActivate, ahk_class CabinetWClass
WinWaitActive, ahk_class CabinetWClass, ,2
If (ErrorLevel)
{
MsgBox, explorer could not be activated
return
}
ExplorerPath := GetActiveExplorerPath()
Loop %I%
{
GuiControlGet, checked,, Ch%A_Index%, Value
If (checked = 1) ; if the control is checked
{
GuiControlGet, file,, Ch%A_Index%, Text
FileCopy, %file%, %ExplorerPath%\, 1 ; overwrite existing files
}
}
return
Move:
Gui, submit, nohide
If !WinExist("ahk_class CabinetWClass") ; explorer
{
MsgBox, No explorer window exists
return
}
WinActivate, ahk_class CabinetWClass
WinWaitActive, ahk_class CabinetWClass, ,2
If (ErrorLevel)
{
MsgBox, explorer could not be activated
return
}
ExplorerPath := GetActiveExplorerPath()
MsgBox, 262180, Move Files, Are you sure you want to move the selected files to`n`n%ExplorerPath%?
IfMsgBox No
return
New_Clipboard := ""
Loop %I%
{
GuiControlGet, checked,, Ch%A_Index%, Value
If (checked = 1) ; if the control is checked
{
GuiControlGet, file,, Ch%A_Index%, Text
FileMove, %file%, %ExplorerPath%\, 1 ; overwrite existing files
}
else
{
GuiControlGet, file,, Ch%A_Index%, Text
New_Clipboard .= file . "`n" ; concatenate the outputs
}
}
If (New_Clipboard = "")
Gui, destroy
else
ClipboardSetFiles(New_Clipboard, DropEffect := "Copy")
return
GuiClose:
Gui, Hide
return
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
GetActiveExplorerPath()
{
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
{
return window.Document.Folder.Self.Path
}
}
}
}
; https://autohotkey.com/boards/viewtopic.php?p=63914#p63914
ClipboardSetFiles(FilesToSet, DropEffect := "Copy") {
; FilesToSet - list of fully qualified file pathes separated by "`n" or "`r`n"
; DropEffect - preferred drop effect, either "Copy", "Move" or "" (empty string)
Static TCS := A_IsUnicode ? 2 : 1 ; size of a TCHAR
Static PreferredDropEffect := DllCall("RegisterClipboardFormat", "Str", "Preferred DropEffect")
Static DropEffects := {1: 1, 2: 2, Copy: 1, Move: 2}
; Count files and total string length
TotalLength := 0
FileArray := []
Loop, Parse, FilesToSet, `n, `r
{
If (Length := StrLen(A_LoopField))
FileArray.Push({Path: A_LoopField, Len: Length + 1})
TotalLength += Length
}
FileCount := FileArray.Length()
If !(FileCount && TotalLength)
Return False
; Add files to the clipboard
If DllCall("OpenClipboard", "Ptr", A_ScriptHwnd) && DllCall("EmptyClipboard") {
; HDROP format
; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40)
hDrop := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 20 + (TotalLength + FileCount + 1) * TCS, "UPtr")
pDrop := DllCall("GlobalLock", "Ptr" , hDrop)
Offset := 20
NumPut(Offset, pDrop + 0, "UInt") ; DROPFILES.pFiles = offset of file list
NumPut(!!A_IsUnicode, pDrop + 16, "UInt") ; DROPFILES.fWide = 0 --> ANSI, fWide = 1 --> Unicode
For Each, File In FileArray
Offset += StrPut(File.Path, pDrop + Offset, File.Len) * TCS
DllCall("GlobalUnlock", "Ptr", hDrop)
DllCall("SetClipboardData","UInt", 0x0F, "UPtr", hDrop) ; 0x0F = CF_HDROP
; Preferred DropEffect format
If (DropEffect := DropEffects[DropEffect]) {
; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40)
hMem := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 4, "UPtr")
pMem := DllCall("GlobalLock", "Ptr", hMem)
NumPut(DropEffect, pMem + 0, "UChar")
DllCall("GlobalUnlock", "Ptr", hMem)
DllCall("SetClipboardData", "UInt", PreferredDropEffect, "Ptr", hMem)
}
DllCall("CloseClipboard")
Return True
}
Return False
}

Surrounding Clipboard with quotes in URL

Warning: I'm very new to this.
I'd like to make Google searches quicker by assigning a hotkey to search a text selection (in web browser) and surrounding that search with quotes (to get exact matches).
I've tried using bits of code I have found, but so far I can only search the selected text on Google, but don't know how to surround the selected text in quotes in the search.
^!d:: ;
prevClipboard := ClipboardAll
SendInput, ^c
ClipWait, 1
if !(ErrorLevel) {
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
If SubStr(ClipBoard,1,7)="http://"
Run, % Clipboard
else
Run, % "https://www.google.com/search?q=" Clipboard
}
Clipboard := prevClipboard
return
This simply opens a google search with the clipboard.
I cannot find out how to make it so "Clipboard" is searched instead of Clipboard.
Any suggestions? Thanks!
Run is a command and variables in commands have to be enclosed in percent signs.
To include an quote, specify two consecutive quotes twice:
Run, http://www.google.com/search?q=""%Clipboard%""
If you want to do a "Phase google search" from any Selected Text (And From Out Everywhere, on your Computer System.)
Then you can try this AHK Script.
you can [Select any Text] and then you can click the [F1] key from your Keyboard Device.
The Computer Find automatic out where it can do the Query Search (us 100x results) and what Browser it must use.
Example1.ahk
;#notrayicon
#SingleInstance force
GroupAdd, Browser, ahk_class Chrome_WidgetWin_1 ; Chrome or Iron
GroupAdd, Browser, ahk_class IEFrame ; Internet Explorer
GroupAdd, Browser, ahk_class MozillaWindowClass ; FireFox
GroupAdd, Browser, ahk_class ApplicationFrameWindow ; Edge
; here you can change the variable into exact search [
quote1 = "
; here you can change the variable into exact search ]
quote2 = "
; here you can change the variables intitle: - inurl: - inanchor: - allinurl: - allinanchor:
insearch = allintitle:
; + = Shift
; ! = Alt
; ^ = Ctrl
; # = Win (Windows logo key)
esc::exitapp ;You can click the (esc) key to stop the script.
f1::
If WinActive("ahk_group Browser")
{
sendinput ^c ;copy the selected text to clipboard memory
sleep 150
sendinput ^t ;CTRL+t make a new tab + goto address bar - use CTRL+L for the active tab + goto address bar
sleep 150
texta = https://www.google.com/?gfe_rd=cr&gws_rd=cr#q=%insearch%%quote1%
textb = %clipboard%%quote2% ;selected text
textc = &lr=lang_us&hl=us&num=100 ; google parameters
clipboard=%texta%%textb%%textc%
sleep 150
sendinput ^v ; paste the selected text
sleep 250
send {enter}
clipboard=%textb%
} else {
sendinput ^c ;copy the selected text to clipboard memory
sleep 150
texta = https://www.google.com/?gfe_rd=cr&gws_rd=cr#q=%insearch%%quote1%%quote1%
textb = %clipboard%%quote2%%quote2%%quote2% ;selected text
textc = &lr=lang_us&hl=us&num=100 ; google parameters
clipboard=%texta%%textb%%textc%
run %clipboard%
clipboard=%textb%
}
return

Ahk Script to find in the current folder a file or a folder with the latest change?

I often save files from Chrome to the Downloads folder or My Documents folder.
You have to look for these files later in the folder. Tell me whether it is possible to somehow make it more convenient using the ahk script. By pressing F3 for example, the last file or folder in the current folder is highlighted. How to implement it. Please help me, I'm a new
F3::
SetTitleMatchMode, 2
File := "" ; empty variable
Time := ""
Loop, Files, %A_MyDocuments%\Downloads\*.*, DF ; include files and folders
{
If (A_LoopFileTimeModified >= Time)
{
Time := A_LoopFileTimeModified ; the time the file/folder was last modified
File := A_LoopFileFullPath ; the path and name of the file/folder currently retrieved
}
}
; MsgBox, Last modified file in %A_MyDocuments%\Downloads is`n`n"%File%"
IfWinNotExist Downloads ahk_class CabinetWClass
Run % "explorer.exe /select," . File ; open containing folder and highlight this file/folder
else
{
SplitPath, File, name
MsgBox, Last modified file = "%name%"
WinActivate, Downloads ahk_class CabinetWClass
WinWaitActive, Downloads ahk_class CabinetWClass
; SelectExplorerItem(name) ; or:
SendInput, %name%
}
return
SelectExplorerItem(ItemName) { ; selects the specified item in the active explorer window, if present
; SelectItem -> msdn.microsoft.com/en-us/library/bb774047(v=vs.85).aspx
Window := ""
Static Shell := ComObjCreate("Shell.Application")
For Window In Shell.Windows
try IfWinActive, % "ahk_id " window.HWND
If (Item := Window.Document.Folder.ParseName(ItemName))
Window.Document.SelectItem(Item, 29)
Return (Item ? True : False)
}
https://autohotkey.com/docs/commands/LoopFile.htm

AHK: closing a window whenever it pops up

I wrote an AHK script designed to copy a text from Adobe Acrobat when I press F9. It then changes it according to a regex and shows the copied text in tooltip. Additionally, I added code to automatically close an annoying window that Acrobat sometimes shows when coping a text, an infamous There was an error while copying to Clipboard. An internal error occurred. When this window doesn't show up, the script keeps showing a tooltip, which is designed to close after a specified amount of time. I've been banging my head against the wall but I don't know how to correct this.
;#NoTrayIcon
#Persistent
#SingleInstance
F9::
#If WinActive("ahk_exe Acrobat.exe")
{
Clipboard:=""
send,^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer,CheckForMsgBox,100
CheckForMsgBox:
IfWinExist, Adobe Acrobat
{
Send {Enter}
SetTimer,CheckForMsgBox,Off
}
;Return
If (StrLen(Clipboard) < 120)
ToolTip % Clipboard
Else
ToolTip Copied
SetTimer, ToolTipOff, -1000
return
}
#If
ToolTipOff:
ToolTip
return
;#NoTrayIcon
; #Persistent ; (1)
#SingleInstance
SetTimer,CheckForMsgBox,100 ; (2)
return
#If WinActive("ahk_exe Acrobat.exe") ; (3)
F9::
clipboard:=""
send,^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard, "\r\n", " ")
If (StrLen(Clipboard) < 120)
ToolTip %Clipboard%
Else
ToolTip Copied
SetTimer, ToolTipOff, -1000
return
#If ; turn off context sensitivity
ToolTipOff:
ToolTip
return
CheckForMsgBox:
; ControlSend, Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText
ControlSend, , {Enter}, Adobe Acrobat ; Close this unwanted window whenever it appears
return
(1) Scripts containing hotkeys, hotstrings, or any use of OnMessage() or Gui are automatically persistent.
(2) SetTimer causes a subroutine (Label) to be launched automatically and repeatedly at a specified time interval.
https://autohotkey.com/docs/commands/SetTimer.htm
(3) Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script.
https://autohotkey.com/docs/commands/_If.htm

SendEvent ^{ins} isn't copying content to the clipboard

!c::
file_name = footnote.ini
restore_original_clipBoard := clipboard
clipboard =
KeyWait, Alt
KeyWait, c ;small c
BlockInput, on
SendEvent, ^{ins} ;^c doesn't work
ClipWait, 2 ; Wait for the clipboard to contain text.
if ErrorLevel
{
MsgBox Failed to save the selection: %clipboard%
exit
}
BlockInput, off
save_selection := clipboard
Problem: Despite a selection being made, Sendevent ^{ins} does not save it to the clipboard. Sometimes I have to repeat my hotkey, alt + c several times before the selection is being copied to the clipboard. The KeyWait should ensure me that only ^{ins} is being processed without any additional keys. What am I doing wrong here?
UPDATE
One of the ways I tried to force copy a selection to the clipboard was by using a while loop. I got it to work through the post: Looping clipboard and errorlevel evaluation not working as expected
PROBLEM
When I make a selection and press alt + c it sometimes gets stuck in the infinite loop that I implemented. But as you can see from that code:
clipboard := ""
while( StrLen(clipboard) < 1 )
{
Send, ^{ins}
Sleep, 50
}
MsgBox % ClipBoard
The infinite loop incorporates within itself a continues resending of ^{ins}. For some reason, my selection is not being recognized as a selection. Whilst it is in that infinite loop, I try to reselect the text. It then recognizes it instantly and copies my selection to the clipboard. But alas! The selection is incomplete because it goes so quick.
This problem is not always like that. Sometimes it recognizes the selection first spot on! So sometimes it copies my selection to my clipboard sometimes not. When it does not, then a resending of a ^{ins} does not seem to work. I do not want to the user to reselect his selection. Is that possible to do?
Send {Ctrl Down}{c}{Ctrl Up}
That presses Ctrl+C, you must do it instantly as one command apposed to pressing Ctrl waiting then pressing C.
Never seen Insert key used for copying text.
Also found this sends Ctrl+C as well.
Send, ^c
To send insert key use
{Insert}
This way works for me:
!vk43:: ; alt+c
clipContent:=ClipboardAll
Clipboard:=""
SendEvent, ^{Ins}
ClipWait, .75
MsgBox, % 262 . (ErrorLevel ? 160:208)
, % ErrorLevel ? "Period expired:":"Result:"
, % ErrorLevel ? "Failed to save the selection.":Clipboard
, % (ErrorLevel ? 0:2) . .5
Clipboard:=clipContent
KeyWait, vk43
Return
!vk43:: ; alt+c
clipContent:=ClipboardAll ; backup clipboard content (if needed)
Clipboard:="" ; no comment :)
Loop
{
SendEvent, ^{Ins}
ClipWait, .75 ; means 750ms, same if write 0.75
; assign value of "ErrorLevel" an variable for further usage
errLvl:=ErrorLevel
; monitoring current action (for debugging purpose only)
TrayTip, % "attempt: #"A_Index
, % """ErrorLevel"" of ""ClipWait"" command is: "errLvl
}
; here you can set the condition of ending the cycle: either...
; ...variable errLvl has not a true value,...
; ...or the number of attempts is equal 5
Until, Not errLvl Or A_Index=5
; value of each field of the command "MsgBox"...
; ...are set depending on the value of errLvl variable...
; ...using a ternary expression
; means if errLvl is a true, "options" field is 262160
MsgBox, % 262 . (errLvl ? 160:208)
; means that "title" has a couple variants
, % errLvl ? "Period expired:":"Result:"
; means that "text" has a couple variants
, % errLvl ? "Failed to save the selection.":Clipboard
; means if errLvl is a true, "timeout" field is 0.5 (500ms)
, % (errLvl ? 0:2) . .5
/* same that and above:
IfEqual, errLvl, % True, MsgBox, 262160
, % "Period expired:"
, % "Failed to save the selection."
, 0.5
Else MsgBox, 262208, % "Result:", % Clipboard, 2.5
*/
TrayTip ; remove "TrayTip" (for debugging purpose only)
; save an positive result (if needed)
IfEqual, errLvl, 0, Sleep, -1, someVar:=Clipboard
; return a temporarily saved data into clipboard (if needed)
Clipboard:=clipContent
KeyWait, % "vk43"
Return
From my experience whenever keystrokes are not recognized reliably it's due to either the system or the targeted program not keeping up with the speed at which those keys are sent.
For SendEvent you could try something like SetKeyDelay, 1000, 1000 and see if this improves things. The other option would be to send explicit down and up keys with intermittent sleep calls as outlined in this answer.