AutoHotkey: Remap Alt + Tab to behave like Win + T - replace

The code below works, with two things I cant figure out how to fix:
1. Default Win + Tab behavior is broken.
2. The Thumbnails are cycled in the wrong order. How could I keep the script as it is now, but also add another shortcut combination (say Alt + `) to cycle in the correct order.
BTW: I have added an {Enter} after Alt is realeased, so the script automatically activates the desired window.
$*Tab::
WinActivate, ahk_class Shell_TrayWnd ;this is used to bring focus out of the current window
Getkeystate, Laltstate, Lalt, P
if Laltstate = D
{
Send {Blind}{LAlt up}{LWin down}{T}
ifequal FixB, 0, Send {T}
FixB = 1
}
else
Send {Blind}{T}
return
~$*LAlt up::
Send {Blind}{Lwin up}{Enter}
FixB = 0
return

You say that this works, but I would expect your Tab key to always produce a T unless the Alt key is down. If you try to insert a tab within a Notepad document, I would expect you to instead type T.
$*Tab:: indicates that hitting Tab will trigger this hotkey, no matter what else is held down. Thus, when you hold down the Win key and hit Tab, you're sending Win+T instead with your else Send {Blind}{T} code.
You are cycling in reverse order because you're using an upper-case T in your Send statement. Send T or Send {T} are sending t along with shift, which reverses the order. Just send t instead, and you'll go in the correct order.

Related

C++ QTextDocument::contentsChange() signal provides invalid input data

I am currently developing a simple oline plain text editor for my university class. I use QTextDocument::contentsChange(index, charsRemoved, charsAdded) signal to determine changes made to document by QPlainTextEdit and send them to the server.
But sometimes arguments, charsRemoved and charsAdded give invalid values.
E.g. when I insert text with Ctrl+V in the middle of the editor's text then proper charsAdded info is reported (charsRemoved == 0 && charsAdded == <inserted_text_length> ). But if I insert text to the zero'th index, right before the first character, then invalid data is given (charsRemoved == <previous_editors_text_size> + 1 && charsAdded == <new_editors_text_size> + 1). However, if I input a single character by typing it via keyboard and my cursor is placed at the beginning of the editor, signal correctly reports reports charsRemoved == 0 && charsAdded == 1
Moreover, when I change my text input language by hitting Ctrl+Shift or when I use Alt+Tab while cursor is placed anywhere in QPlainTextEdit window,
then signal QTextDocument::contentsChange() is emmited twice and it reports that charsRemoved == <current_editors_text_size> + 1 &&
charsAdded == <previous_editors_text_size>
Is it a bug or otherwise how can I handle this issue?
It looks like this bug have been reported since qt 4, but was never fixed.
The latest bug report I could find is this one.
For the double signal emission, consider that this signal should be used to report both changes in text contents and format. So maybe those combinations of keys trigger two signals, once for the text and once for the change in the input mode. But this is just my supposition.

Autohotkey increase/decrease last number in selected text

I'm trying to make a useful system to automatically increase numbers in selected text. For example, when I working in Zbrush on the project named "Example_character012" I have a folder with 23 files from "Example_character012_prj_01.zpr" to "Example_character012_prj_23.zpr". When I need to save next iteration I'm pressing "ctrl+s" and then regular saving screen opens in the last working directory with preselected filename text, in this scenario it would be "Example_character012_prj_23.zpr". I need to copy this text, select the last number(23) increase it by one, and past it all the text back to the text-field. In this case it would be "Example_character012_prj_24.zpr".
This kind of script would be really useful in all kinds of scenario, mainly with saving in different programs.
I tried to made it myself and come up with this:
^!Numpad1::
Sleep 100
Send ^c
Sleep 10
RegExMatch(clipboard,"(\d+)(?!.*\d)",Number)
NewNumber:=Number1+1
OldNumber:=NewNumber-1
Clipboard:=RegExReplace(clipboard, OldNumber, NewNumber)
Sleep 10
Send ^v
Sleep 10
Send +{Home}
Return
^!Numpad0::
Sleep 100
Send ^c
Sleep 10
RegExMatch(clipboard,"(\d+)(?!.*\d)",Number)
NewNumber:=Number1-1
OldNumber:=NewNumber+1
Clipboard:=RegExReplace(clipboard, OldNumber, NewNumber)
Sleep 10
Send ^v
Sleep 10
Send +{Home}
Return
Ctrl+alt+Num1 for increase and Ctrl+alt+Num0 for decrease.
It's work fine with in the simplest scenario, but have significant problems I can't solve myself:
It only works in english keyboard-layout. When I use it in russian layout - it puts "cv" instead of pasting(If I paste manually afterwards it put the correct text)
Sometimes it increases not only the last number "Example_character012_prj_01.zpr" changing to "Example_character022_prj_02.zpr" instead of "Example_character012_prj_02.zpr"
Zeros working incorrectly "Example_prj_09.zpr" changing to "Example_prj_010.zpr" instead of "Example_prj_10.zpr"
After pasting it selects the whole line instead of only what just have been pasted
If it's possible, it would be great to make it work without affecting clipboard at all.
Sometimes some keys like "ctrl" just get stuck until being clicked manually for some reason.
Edit:
Got some help at Autohotkey forums, current version of script below. Only questions 4 and 5 left.
^!Numpad1::
^!Numpad0::
Sleep 50
Send {Ctrl up}^{Ins}
Sleep 10
RegExMatch(Clipboard, "^(.*)(?=_)_(\d+)(\....)$", Match)
Len := StrLen(Match2)
if InStr(A_ThisHotkey, 1)
Match2++
else
Match2--
if (StrLen(Match2) < Len)
Match2 := Format("{:0" Len "}", Match2)
Clipboard := Match1 "_" Match2 Match3
Sleep 10
Send +{Ins}
Sleep 10
Send +{Home}
Return
The reason your CTRL key is misbehaving is maybe because:
If you have a hotkey like
^F12::
Send {a}
What actually happens when you hit CTRL+F12 is that AHK releases CTRL, sends a, then re-hits CTRL.
This can get more complicated, eg in your scenario.
Try prefixing your Send commands with {Blind} eg Send {Blind}^v to suppress this behavior.
This may or may not work (ie if the hotkey is shift+ctrl+something but the hotkey sends ctrl+v, then what would actually get sent is ctrl+shift+v) but if it does still work, it may make CTRL start behaving itself more.

how to flush or clear GetAsyncKeyState's buffer

i'm using GetAsyncKeyState in an MFC application to check if Esc button is pressed,
but when i press on Esc button from dialog and use GetAsyncKeyState in a different dialog it returns nonzero because it's exists in the message queue.
how can i clear or flush GetAsyncKeyState's buffer or delete this message from message queue?
Thanks in advance.
The direct answer to your question would be just calling it a second time, discarding the value from the first time.
But I guess that what you really want to know is how to read the current status of the key, regardless of when you last checked. Since you wrote "returns nonzero" I believe you are not using it correctly.
You need to check for the bit with value 0x8000 because this one indicates whether it's pressed right now. The bit with the value 1 is the one which is set if the key was pressed since the last check, and that's the one tripping you over, so just ignore it and directly test for the bit with value 0x8000.
Example code:
if(GetKeyState(VK_RETURN) & 0x8000) yayReturnIsPressed();
Checking if(GetKeyState(VK_RETURN)) or if(GetKeyState(VK_RETURN) != 0 will not do what you want because it will be fulfilled if any of the bits in the return value are set.
In GetAsyncKeyState documentation you can read:
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
(emphasis mine)
so to check current state of ESC button you should only check most significant bit:
bool isEscPressed = GetAsyncKeyState(VK_ESCAPE) & 0x8000;
if you check state like that: if (GetAsyncKeyState(VK_ESCAPE)) {} then it will enter if statement even if ESC is not currently pressed.

movement binding in c++ using ncurses

I can't get this movement binding to work. I'm using the ncurses library, update_ch and oldch are global variables. KEYERR is a macro set to -120 (I just don't handle those keypresses). I'm trying to restrict the player so he can't hold up, down, left, or right, but he has to keep pressing them to move. It's not working, you can still hold the keys down and move. Any suggestions? My logic must be off.
if(update_ch != KEYERR)
{
oldch = update_ch;
}
update_ch = getch();
if(oldch == update_ch)
{
update_ch = KEYERR;
}
I'm trying to restrict the player so
he can't hold up, down, left, or
right, but he has to keep pressing
them to move.
I am pretty sure this isn't possible with curses. If I remember correctly curses only receives characters from a terminal. It doesn't control anything about the process.
Measuring the time between to such readings might give you a hint if the user is holding a key instead of continuously pressing. I mean, when you do a reading, record the following
Key read
Time of read (millisecond precision)
When you read a value, ask the following:
Is it the same as the last key ?
What's the difference between the current time and the time of the last read ?
If it's the same key and the time difference is smaller than some threshold you can decide he's holding the key down.

Using SetKeyboardState along with GetKeyboardState in C++

I don't know how to write a good question here, but, basically, does anyone know where I can possibly find some C++ source code using these to actually set keyboard state? For some reason using it the way MSDN does on Windows 7 doesn't do...anything at all.
Basic code:
PBYTE keyState;
GetKeyboardState(keyState);
...
// Later on when I need to set the keyboard state (key pressed etc) back to original:
SetKeyboardState(keyState);
and ... nothing happens :(
From:
http://www.gamedev.net/community/forums/topic.asp?topic_id=43463
First off, GetKeyboardState() would be the wrong function to use because as Windows has a chance to process keyboard messages (whether you want it too or not) it updates the results of the keyboard's state for the next call to GetKeyboardState().
Here's a little function that I use to get the status of the keyboard's keys. Be carefull though, depending on how fast your main loop is, it may cause problems if you aren't expecting it.
You need to keep track of whether or not a specific key was pressed the last time you called the ReadKeyboard() function. If your loop polls the keyboard 30 times a second, then pressing a key once probably causes the key to be flagged 3 or 4 calls in a row. Rather confusing sometimes. Just thought I'd mention it.
void ReadKeyboard( char* keys )
{
for (int x = 0; x < 256; x++)
keys[x] = (char) (GetAsyncKeyState(x) >> 8);
}