Basically want to make a macro script

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 13 14:15:37 PST 2014


On Thursday, 13 November 2014 at 21:56:48 UTC, Casey wrote:
> 1) Which compiler should I use?

I use the digital mars D. It is pretty easy from the command 
line, you put the code files in a directory then pop open a cmd 
prompt in that folder. If the compiler is installed, you should 
be able to just type "dmd your.d list.d of.d files.d" and it 
spits out an exe.

The command for the program I gave is

dmd hotkey.d simpledisplay.d color.d -L/SUBSYSTEM:WINDOWS:5.0:

You would just copy/paste that into the command prompt from your 
folder. dmd is the name of the compiler, then the list of source 
files, and the last SUBSYSTEM bit is telling it to produce a 
Windows GUI program instead of a text based program.

> 2) I can't figure out what the heck half of this code means.

yeah, Windows programming can be a bit weird even for experienced 
coders. Let me come back to you and explain each line in a future 
message.

> 3) I'm sure that everything you have in there has a meaning, 
> but it looks over complicated to me.  Shouldn't it look 
> something like this?
>
> [code]
> void main() {
>     import std.stdio;
>     import simpledisplay;
>     import *Others that need to be imported*;
>     if (*hotkey command here*) {
>     then writeln ("We're losing Alpha!")
>     return 0;
> }
> [/code]

It potentially could look like that if the other underlying code 
was already written in a library or something, but you said you 
looked for an existing program to do what you want and couldn't 
find it, so here we're writing that underlying code!

Any program that listens for multiple user events tends to be a 
bit more complicated than that though because you want to loop, 
waiting and reacting to several events, instead of always going 
forward.

That's why my thing ends with window.eventLoop() instead of plain 
return - it keeps the window up until it is closed, reacting to 
various events.

That line with handleNativeEvent sets up the reactions to those 
events. The operating system sends our window messages when 
things happen, and we run stuff in response to those messages.

Since this program only cares about hotkeys, the only handled 
case is the WM_HOTKEY message, and in response to it, we send a 
string. All the other code surrounding those lines are just 
telling the library to do default behavior for the other messages 
(for example, close when the user clicks the X, or draw itself 
when it is minimized and restored).



The rest of that file consists of two other parts: the sendString 
function, which creates the keyboard events to type out the 
message and forwards them to the operating system, and then the 
copy/pasted definitions of operating system functions (starting 
at the "import core.sys.windows.windows;" line) so we can call 
them.

D doesn't come with all Windows functionality built in. It can 
use it all, but you often have to tell it what the functions' 
names and arguments are (or download a file that has this done 
already). That's all I'm doing in the second half of the program 
- that's copy/pasted info from Microsoft's documentation.

> Could you tell me which keys you used for the hotkey in your 
> sample code?  I can't figure it out, but my guess it alt + c?  
> Not sure though.

In my sample, the hotkey is set to F2. It is set on this line:

         if(!RegisterHotKey(window.impl.hwnd, hotkey_id, 0, 
VK_F2)) {

The 0 in there means you don't have to hold ctrl, alt, or 
anything to trigger it. Then VK_F2 represents the F2 key (each 
key has its own "virtual key code" in Windows which you can find 
on a table on the Microsoft website. But the basic pattern is VK_ 
(for Virtual Key) then the name.


More information about the Digitalmars-d-learn mailing list