How to detect whether a "text/writeable" field is focused - autohotkey

How do I detect whether a text/writable field is focussed?
By text/writable field, I mean e.g. notepad; this SO-text field; the address-bar of Chrome, etc. Basically anything that you are supposed to write into.

Sounds like you want ControlGetFocus, it lets you get which control of a target window that has input focus. What kind of window will really make a difference, IE is pretty doable, Notepad++ also doable, Chrome would require special code for example: https://autohotkey.com/board/topic/103178-how-to-get-the-current-url-in-google-chrome/#entry637687.
Example of getting the control of the active window that works pretty well:
!z::
activeWindow := WinExist("A")
ControlGetFocus, focusedControl, ahk_id %activeWindow%
if(ErrorLevel) {
MsgBox, Couldn't get focusedControl
} else {
ControlGetText, focusText, %focusedControl%
if(ErrorLevel) {
ControlGetText, focusText,, ahk_id %activeWindow%
Msgbox, Focused window text: %focusText%
} else {
Msgbox, Focused control text: %focusText%
}
}
return
So you would want to find out the identifier of what ever field you were interested in and check if it has focus when you want to know, or maybe regularly with SetTimer.

Related

Using keyboard and external numpad in AutoHotInterception

What I need is to map my keys from external numpad to keys on my keyboard. I've decided to use the AutoHotInterception program from evilC. The overall goal is to be able to use windows mouse keys with my keyboard. I have come to the point where program registers both inputs from my numpad and keyboard and I can type numbers with my keyboard but it doesn't really affect the windows mouse keys.
This is what I have so far:
#SingleInstance force
#Persistent
#include Lib\AutoHotInterception.ahk
AHI := new AutoHotInterception()
keyboardId := AHI.GetKeyboardId(0x04D9, 0x8008)
numPadId := AHI.GetKeyboardId(0x0C45, 0x7663)
AHI.SubscribeKeyboard(numPadId, true, Func("KeyEvent"))
AHI.SubscribeKeyboard(keyboardId, true, Func("KeyEvent"))
return
KeyEvent(code, state){
ToolTip % "Keyboard Key - Code: " code ", State: " state
if (state) & (code=30)
{
Send, {NumpadUp}
}
}
^Esc::
ExitApp
The main problem is that you need to send input at hardware driver level to trigger WMK.
Then I'd like to say that your questions seems quite weird. It seems to contradict itself?
You want to remap keys from the external keyboard, but then you also say that you just want to use the numpad with your main keyboard? So what is the external keyboard for actually?
In any case, Here's some advise and a revised script you can probably adapt for your own use. So firstly there's really no need to subscribe to the whole keyboard. Just subscribe to the keys you want.
And since I'm quite unclear on what it is you're actually trying to do, I'll just show an example of how you'd remap A, S and D to Numpad1, Numpad2 and Numpad3 at hardware driver level.
#include Lib\AutoHotInterception.ahk
AHI := new AutoHotInterception()
;kind of optional
;if you don't switch up stuff, you'll always have the same id
keyboardId := AHI.GetKeyboardId(..., ...)
;binding arguments to the function object to make use of the same function over and over
;so don't need to define a new function for each key
AHI.SubscribeKey(keyboardId, GetKeySC("a"), true, Func("KeyEvent").Bind(GetKeySC("numpad1")))
AHI.SubscribeKey(keyboardId, GetKeySC("s"), true, Func("KeyEvent").Bind(GetKeySC("numpad2")))
AHI.SubscribeKey(keyboardId, GetKeySC("d"), true, Func("KeyEvent").Bind(GetKeySC("numpad3")))
return
;the key argument will be the argument we bound to the function object above
;the AHI library takes care of passing in the state argument
KeyEvent(key, state)
{
global keyboardId
AHI.SendKeyEvent(keyboardId, key, state)
}
^Esc::ExitApp
Sending input is documented here here.
(Side question, any chance you're doing this for OSRS?)

How do I send a message to a window handle using AutoHotKey?

I'm trying to detect if ffdshow is currently running on the computer using AutoHotKey.
Someone has suggested that I can achieve this by sending a message to the to the ffdshow window handle. If it succeeds, then ffdshow is running.
According to ffdshow, the window handle is 32786 and, according to the AutoHotKey documentation, I want to use PostMessage and then check ErrorLevel.
However at that point, I'm struggling to understand the documentation. I've got the following:
ControlHwnd := 32786
VarContainingID := 32786
PostMessage, 0x00, , , ,ahk_id %ControlHwnd%, ahk_id %VarContainingID%
MsgBox %ErrorLevel%
but that always reports a 1 indicating that it was unable to connect to the window handle - even though ffdshow is running.
I've also tried changing PostMessage to the blocking SendMessage but that always reports FAIL.
I'm clearly doing something wrong, but I'm not really sure what. Can anyone help?
Thanks to blackholyman and MCL, I found the problem.
After digging around in the sample code found here, it turns out there is a window class for ffdshow called ffdshow_remote_class.
As a result, the following code:
DetectHiddenWindows, On
WinGet, activeid, ID, ahk_class ffdshow_remote_class
MsgBox activeid = %activeid%
will return a hWnd value for ffdshow (stored in activeid) if it is running or nothing if it is not.

SDL 2.0 Quit Event with Multiple Windows

I'm using SDL 2.0, and decided to try out making multiple windows. Unfortunately, now I can't quit out of my program without going back to the IDE and force closing it.
The event handling is as simple as possible, I am only polling for the quit event, and it worked perfectly fine before I added the second window. Is the Quit Event ignored when using multiple windows? If so, how can I turn it back on?
The Quit Event is only sent when the last open window is trying to close, otherwise a window close event is sent.
I also ran into this problem, and the documentation is a little sparse on the topic so I ended up here.
The summary of the problem is:
If you have only one Window, clicking the X button will trigger an SDL_QUIT event.
If you have two or more windows, clicking the X button will trigger an SDL_WINDOWEVENT event, with an internal type of SDL_WINDOWEVENT_CLOSE.
So if your typical code for single-window quit events might look something like this:
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
// ... Handle close ...
}
}
The multi-window equivalent would be:
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_WINDOWEVENT
&& e.window.event == SDL_WINDOWEVENT_CLOSE)
{
// ... Handle window close for each window ...
// Note, you can also check e.window.windowID to check which
// of your windows the event came from.
// e.g.:
if (SDL_GetWindowID(myWindowA) == e.window.windowID)
{
// ... close window A ...
}
}
}
Note that on the last window, you will again receive SDL_QUIT because it's now the only active window - so best to structure your code in a way that correctly handles both depending on the circumstances.
See docs for more info.

GAMBAS - Exit Sub in IF statement

I have a checkbox that is supposed to trigger whether or not a button is visible. Below is the code:
PUBLIC SUB chkGiveUp_Click()
' Check to see if the Give Up button's visible property is set to true, and if it is, hide the button. If it is hidden, show it again.
IF btnClearAnswer.Visible THEN
btnGiveUp.Visible = FALSE
RETURN
END IF
IF btnGiveUp.Visible = FALSE THEN
btnGiveUp.visible = TRUE
RETURN
END IF
END
However, it is not exiting the sub properly and therefore one if contradicts the other. What is the proper way to do this? I am a beginner gambas programmer transferring over from VB6. I'm running gambas2 on Ubuntu 11.10, and the project type is a graphical application.
I think if you put the "return" after the "end if" it might work the way you want it to...
Otherwise,
IF btnClearAnswer.Visible THEN
btnGiveUp.Visible = FALSE
Else btnGiveUp.Visible = FALSE THEN
btnGiveUp.visible = TRUE
END IF
RETURN
You'll have to double check my command names though, since I don't know GAMBAS... but these languages are all enough alike that I can see the problem... by both statements just being 'IF'... they are both being ran... (check and make sure "RETURN" is the right command also... some languages use "RET") if it were my program and that didn't work I would just switch to a select case

Messagebox.show fires twice after button click (MVVM)

I am using Galasoft MVVMLight. I have a button bound to a command which sends a message to the view to display a messagebox asking for confirmation. If I click either the Yes or No on the messagebox it performs the necessary actions then shows up again. However if I step through the program instead I only get the messagebox once. Is this a bug or is something else going on?
EDIT: I modified the messagebox.show line by adding an Icon and default result and now I can't reproduce this behavior... I'm stumped... if it happens again I'll try a counter like airplaneman19 suggested.
Try tracking the amount of times the MessageBox shows up with an integer, like so:
int counter = 0;
if(counter == 0){
MessageBox.Show();
counter++;
}
else if (counter == 1)
/*Do something that won't alter the program just to escape the if....else statement
like "x++";
I had a similar problem once, I mean, with MessageBox firing twice. It was due to focus changes, and ListView in WinForms fired another selection changed event when running the app; but when debugging - some focus change was missing, and there was no bug :)
I hope this atleast gives you some ideas...