AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sending keys to another script / while retaining modifiers

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Sephra



Joined: 11 Apr 2005
Posts: 4
Location: So-Cal

PostPosted: Tue Apr 12, 2005 2:03 am    Post subject: Sending keys to another script / while retaining modifiers Reply with quote

Before I ask my question let me say that this is a truly awesome piece of software; kudos and thanks to all involved.

This is my first time doing any sort of coding. I've spent the majority of my time these past two weeks trying to teach myself scripting (I'm very bad at it). I've managed to make some headway and get past previous problems, but after scouring the documentation and searching these forums for half a day I'm no closer to overcoming my latest hurdle(s).

So my first question is, is there a way to have an ahk script (continuously) intercept and interpret the keystrokes of another ahk script? (further explanation below)

My next question is, is there a way to send modifiers or additional keys along with a hotkey (without including them in a hotkey's definition)? (further explanation below)

For context, here are the relevant parts of my script:

Code:

; script.ahk
; this is part of my all-inclusive script that will run continuously from logon to logoff
; it runs dvorak.ahk (below) when scrolllock is toggled on

#usehook

~scrolllock::
getkeystate scroll,scrolllock,t
if scroll = d
   run c:\documents and settings\admenstruator\desktop\temp backups\dvorak.ahk
setscrolllockstate on
return



Code:

; dvorak.ahk, run by the above script when scrolllock is on
; this is just a sample segment, for one key out of 33
; it exits when scrolllock is toggled off

#usehook

scrolllock::
setscrolllockstate off
exitapp

k::send t
^k::send ^t
!k::send !t
^!k::send ^!t
+k::send +t
^+k::send ^+t
!+k::send !+t
^!+k::send ^!+t
#k::send #t
; send "t" when "k" is pressed, "Ctrl + T" when "Ctrl + K" is pressed, etc.

; repeat ad nauseum


I suppose a bit of explanation is in order. I'm trying to have my computer run in Dvorak when scrolllock is on and Qwerty when it's off, which would make my roommates/other users of my computer very happy. Using Windows' built-in keyboard layout tools requires pressing wierd key combinations, there's no indicator light, and it only applies changes to the current window (or desktop). And if there's a program that can do something like this, I'd expect it to be AHK.

My scripts are currently bug free. However, I have the feeling I'm missing something by making 9 hotkeys each for 33 keys (but if I don't, "Alt + K" won't send "Alt + T," and so on). More importantly, it does not work in conjunction with the following piece of script:

Code:

; also part of script.ahk
; for starters, this makes the Dvorak keys CHTN (Qwerty IJKL) into arrow keys when capslock is on.
; (it does the same for several left-hand keys and the home, end, pgup, and pgdn keys)
; also, escape and capslock have been swapped (damn handy for Vim and everything else)

#usehook
setcapslockstate alwaysoff

hotkey t,off
hotkey ^t,off
hotkey +t,off
hotkey ^+t,off

t::send {down}
^t::send ^{down}
+t::send +{down}
^+t::send ^+{down}

*capslock::
hotkey t,on
hotkey ^t,on
hotkey +t,on
hotkey ^+t,on
keywait capslock
if a_thishotkey = *capslock
   send {esc}
hotkey t,off
hotkey ^t,off
hotkey +t,off
hotkey ^+t,off
return

esc::
getkeystate lock,capslock,t
if lock = u
   setcapslockstate alwayson
if lock = d
   setcapslockstate alwaysoff
return


If I did the preceding section any other way (e.g. capslock & t::send {down} ) it would turn the capslock on whenever other keys were pressed, hence that style of code. It works wondrously on its own, but when I switch on Dvorak with the previous scripts, it doesn't capture the right keystrokes, obviously.

Now, I know there are ways of working around these problems (such as appending a remapped version of the prior script to dvorak.ahk) but before I actually make key bindings for everything, I have to ask if there are more efficient methods of doing so.

After looking around the documentation, my guess is "no" to both of my prior questions (if so may they be added to the wish list?). In terms of my current project it's possible, if not efficient, to work around them, but of all the useful functions AHK ought to be able to do, I'd think this sort of keystroke manipulation would be top priority.

Apologies for such a long post, and I certainly don't mean to come off sounding disappointed with the program. Thanks in advance for all your help.

I will conquer you yet, Capslock! Next up, 101 uses for the accent/tilde key.
_________________
"Truly great madness cannot be achieved without significant intelligence." --Henrik Tikkanen
Back to top
View user's profile Send private message
dijiyd



Joined: 31 Mar 2004
Posts: 90
Location: Philippines

PostPosted: Tue Apr 12, 2005 2:00 pm    Post subject: Reply with quote

Possibly using '*' as modifier. More details here.

I really don't know, I haven't tried yet, maybe later. (Though I think the other guys would know the answer, especially chris.)
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Wed Apr 13, 2005 12:34 pm    Post subject: Re: Sending keys to another script / while retaining modifie Reply with quote

Sephra wrote:
is there a way to send modifiers or additional keys along with a hotkey (without including them in a hotkey's definition)?

... making 9 hotkeys each for 33 keys (but if I don't, "Alt + K" won't send "Alt + T," and so on).
There is a plan to have a SendBlind command, which would probably allow you to simplify your script. Currently, the way you're doing it is probably best because as you've seen, the normal Send command is a little too aggressive about changing the state of Ctrl/Shift/Alt/Win in cases like these. For example, if *k::SendBlind t were a hotkey, SendBlind would know not to release the modifiers you're physically holding down; that is, it would simply send a "t" keystroke and nothing more.

As for the rest of your post, I think I understand most of it. Here are a few things that might help:
1) In case you weren't sure, the Hotkey command does not affect hotkeys in other scripts.
2) The most recently started script's hotkeys will override any identical hotkeys in use by currently-running scripts.
3) You might try experimenting with the Suspend command as a method of combining your two scripts into one. Suspend allows all hotkeys except those you explitly exclude to be disabled and later re-enabled with a single command. Due to the complexity of this case, Suspend might not be feasible, it's just something to consider.

Quote:
is there a way to have an ahk script (continuously) intercept and interpret the keystrokes of another ahk script? (further explanation below)
It could be done with the Input command, but I doubt that's the effect you're after. I don't have a full enough understanding becuase I haven't battled with this particular remapping challenge the way you have. If all else fails, I might actually have to use the scripts to really understand it.

Quote:
After looking around the documentation, my guess is "no" to both of my prior questions (if so may they be added to the wish list?).
... of all the useful functions AHK ought to be able to do, I'd think this sort of keystroke manipulation would be top priority.
I don't understand the high-level goal well enough. You've given a great amount of detail in your description but some of it is at too low a level for me to understand without gone through the process you did. Perhaps this is not an issue because after reading this post you may get new ideas of how to solve the problems. But if not and you think a new feature or two would be a great benefit, please give a high level explanation of what the feature would do from the user's point of view (such as what it's syntax might look like), and what its cause-and-effect would be.

Thanks.

Edit: Fixed typo.


Last edited by Chris on Thu Apr 14, 2005 1:00 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
Guest






PostPosted: Thu Apr 14, 2005 6:07 am    Post subject: Reply with quote

@dijyid -- The * prefix doesn't help, because *k::send t doesn't imply ^k::send ^t, and so on for other modifiers and combinations thereof.

In other news, Sendblind is exactly what I was looking for/was going to suggest. I've already finished coding my "dvorak.ahk" (had fun doing it because I wrote a reusable ahk script to help me write it). It's a sufficient stopgap; the only thing I'm worried about at this point is efficiency. Would having ~36 hotkeys (arrows, home, end, pgup/dn) relabel each time the capslock is pressed and again when it's released affect performance much? I don't notice anything, but what do I know.

So that problem is (will be) mostly taken care of. My second issue was , I suppose, having hotkeys propagate/inherit from other scripts. Granted, it's probably just as well to rewrite the code in another script or consolidate them (which is what I did). This I was wondering also mostly for reasons of efficiency. However, I have for the most part gotten over my least-cluttered-code-as-possible obsession and just coded the whole, empty, 500-line script. In retrospect, this feature doesn't seem as necessary a feature to implement in subsequent versions--having "global" hotkeys seems like more baggage for a script to check when it loads up, a big price to pay for the convenience of a coder.

I was going to make a post in the Wish List forum once I better understood just what I wished for, but since it seems the Sendblind feature has already been thought of and global hotkeys aren't necessary, so consider your job done here. Thanks again!
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Thu Apr 14, 2005 1:08 pm    Post subject: Reply with quote

Quote:
the only thing I'm worried about at this point is efficiency. Would having ~36 hotkeys (arrows, home, end, pgup/dn) relabel each time the capslock is pressed and again when it's released affect performance much?
Changing the labels of many hotkeys should be a pretty fast operation. By contrast, disabling and enabling individual hotkeys can sometimes be a little slower depending on whether it needs to add or remove the keyboard and/or mouse hook.

Quote:
Sendblind is exactly what I was looking for/was going to suggest.
Thanks for the confirmation. Hopefully SendBlind will be added soon.

Edit: Send {Blind} has been added in v1.0.40, which supports the new method of remapping keyboard keys and mouse buttons.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group