Autohotkey: problem with minimizing windows - windows-10

I create two windows through autohotkey and move them to my leftmost and rightmost monitor respectively (I have 3 monitors).
When I minimize them, and call them again, they are being shown on the central monitor. But I want them to be shown where they were before.
See following gif.
Following is the ahk script:
Run, Notepad
WinWait, ahk_class Notepad
WinSetTitle, asd
Run, Notepad
Sleep, 1000
WinGet, fensterID, List, ahk_class Notepad
Loop, %fensterID% { ; will run loop for number of windows in array
WinActivateBottom, % "ahk_id " fensterID%A_Index%
WinGetTitle, title, A
MsgBox, %title%
if (title == "asd")
WinMove A,, 1500, 0
else
WinMove A,, -2304, 0
WinMaximize, A
}

Meanwhile I switched to AutoIt and - having the same problem there - I found out, thanks also to this, that it works if I disable the maximized option for Windows Terminal (the actual program I was trying out with), then move and maximize the window. It should do the same with AutoHotKey.

Related

autohotkey - SetTitleMatchMode, RegEx match all windows not working

I'm trying to create a shortcut to activate the bottom-most window, where the cursor is pointing. In the screenshot below, we have 3 windows placed one on top of the other. When I press Ctrl-d, it should activate the last window at the back. Because the mouse cursor goes through all 3 windows.
This is what I have coded so far,
^d::
; (1) this works
; SetTitleMatchMode, 2
; WinActivateBottom, Notepad2; Activate the least recent window
return
(1) this works perfectly, when I specify the window title "eg. Notepad2".
^d::
; (2) make it work with all windows (DOES NOT WORK)
SetTitleMatchMode, RegEx
WinActivateBottom, .* ; Activate the least recent window
return
(2) this does not work. I'm trying to make it work with ANY windows regardless of the window title. Is WinActivateBottom explicitly not allowing RegEx?

Changing focus with Autohotkey, what is going on?

I just want my script to select the right Window to input key storkes. To do this I think I use WinActivate. I ran the example from their site but found some strange results in Windows 10
IfWinExist, Untitled - Notepad
WinActivate ; use the window found above
else
WinActivate, Calculator;
If Notepad is minimized, it gets the focus
If Notepad is open but is not have the focus (i.e. another window is on top of it), Notepad gets the focus
If Notepad is not open and the calculator is minimized, for some strange reason it doesn't get the focus.
If Notepad is not open and the calculator is open but not have the focus, it gets the focus.
What is causing the inconsistency?
It happens because of the implementation of the WinActivate function on the language. WinActivate tries to open an window, but it might not be able to.
From the documentation
Six attempts will be made to activate the target window over the
course of 60ms. Thus, it is usually unnecessary to follow WinActivate
with WinWaitActive or IfWinNotActive.
Usually you can try the #WinActivateForce directive in combination with WinWaitActive or IfWinNotActive.
Sometimes you can use an ahk_exe parameter to match the window. It may work in cases where the window title doesn't. In this case you would use
Also it's useful to try restoring the window with WinRestore.
SetTitleMatchMode, 2
IfWinExist, Bloco de notas
{
WinActivate ; use the window found above
}
else
{
WinRestore, ahk_exe calc.exe
WinActivate, ahk_exe calc.exe
}
I'm on Win7, but the above worked for me.
Here is an example of a good and complete implementation of a function that tries to activate a window.
Semicolons introduce comments in AHK. There has to be a white space before it, otherwise it's seen as part of the string (alias winactivate "Calculator;")
so, use
WinActivate, Calculator ;
or just omit the ;, for it doesn't contribute anything

Sending key strokes to a window

I want to use AutoHotkey to run a program and send some keystrokes.
But I don't get it to work. The program pops up, and then nothing happens.
Here is what I have so far:
run, c:\windows\sysnative\gfxv4_0.exe
WinActivate, "Intel(R) Graphics Control Panel"
SendInput {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Enter}
Just for testing, I have also tried to send some keys to notepad, but that doesn't work either. The notepad window pops up, but then nothing:
run notepad.exe
WinActivate "Unbenannt - Editor"
SendInput abc{Tab}{Tab}xyz
I have also tried WinActivate without parameters, but no results either. Have also tried with Send, SendPlay, SendEvent and SendRaw. No change.
System is Windows 8.1 64 Bit, latest Service Packs.
Core i7, 8 GB, 2 screens.
Remove the quotes
The parameters of AHK commands are literal strings unless specified otherwise in the help for a command or you explicitly make the parameter an expression: WinActivate, % "some string"
Wait for the window to appear before activating it because launching an application takes time:
WinWait Intel(R) Graphics Control Panel
Maybe there's ® instead of (R). Use the exact window title text from Window Spy application that comes with AutoHotkey.

Clear PyCharm Run Window

Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

Identify Special key keycode

I am using a windows 8 laptop. My old laptop had a key combo, fn+f5, which would make the computer go to sleep. My new laptop has a power button on the side that acts like the one on your phone, it puts the PC to standby/sleep. But i don't like hardware buttons because they wear out.
I plugged in a logitech keyboard that has a dedicated key with the power button symbol and when you press it, it does the same thing. (i did not install any software or drivers btw, just plugged it in) My goal is to use autohotkey to assign a key combo to send the same keycode. There are baked-in some windows functions (I don't know what to call them. Shell commands?) that are supposed to put the PC to sleep but they always put it into hibernation or shut it down, not what i want, which is why i am resorting to this. Does anyone know how to intercept keycodes or something?
Paste this into a text file and save as .AHK and run.
#SingleInstance Force
#InstallKeybdHook
#InstallMouseHook
NP:="Untitled - Notepad"
IfWinExist, % NP
WinActivate
else
IfWinNotExist, % NP
Run, Notepad.exe,, UseErrorLevel, nPID
nPID=%nPID%
if (ErrorLevel){
MsgBox, there is a problem
}else{
ToolTip, Notepad running as PID: %nPID%
Sleep, 1000
ToolTip
}
WinActivate, % NP
WinWaitActive, % NP
Send, {Raw}Hello %A_UserName%,`n`nRight click on the H icon on your traybar`ngoto -> "Open" -> "View" - > "Key History and Script Info"`n`nThis area shows you your past keypresses. =]`n`n-islanq
return
AHK Literature on Special Keys - Link
AHK Literature on InstallKeyboardHook - Link

Resources