macro program

This is a discussion on macro program within the C++ Programming forums, part of the General Programming Boards category; I want to make a program that enables a user to create a macro whenever they want, even if they ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    macro program

    I want to make a program that enables a user to create a macro whenever they want, even if they are not within the program window. For example, as long as the program is running (could be minimized), the user could create the macro if they were in say...Microsoft Word.
    So, I guess my question is, how do I make the program work, even if the user isn't in the program window?
    This would be a console application.

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I used RegisterHotKey() to make a hotkey that works even when the window does not have focus. Only thing is if another program has the same combination it won't work. It's declared in windows.h. Might not be exactly what you're looking for, but maybe it will help.
    Code:
                //Register hotkey with the system
                RegisterHotKey(hwnd,HOTKEY_TAB,MOD_CONTROL,VK_TAB);
                RegisterHotKey(hwnd,HOTKEY_E,MOD_CONTROL,VkKeyScan('e'));

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Does this work for a console application?
    I was trying to get it to work:
    Code:
    #include "windows.h"
    #include <iostream>
    using namespace std;
    
    
    BOOL RegisterHotKey
    (
        HWND hWnd,        
        int id,           
        UINT fsModifiers,  
        UINT vk           
    
    );
    
    
    int main()
    {
    	/*
    	if( certain key combo pressed )
    	{
    		cout << "Key has been pressed." << endl;
    
    	}
    	*/	
    
    	return 0;
    }
    I'm getting the error:
    Error 1 error C2373: 'RegisterHotKey' : redefinition; different type modifiers

    Thanks
    (Edited)
    Last edited by Cpro; 08-01-2008 at 11:29 AM.
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, considering you're not using it at all, I would have to doubt you know what you're doing. Read MSDN as well as the post above yours. It should explain how to use it.

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "Well, considering you're not using it at all, I would have to doubt you know what you're doing."
    Of course i don't; that is why i posted the question.


    Anyways, I have it working like so:
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	RegisterHotKey(NULL, 666, MOD_CONTROL, 'Q');
    
    	for(;;)
    	{
    		MSG Msg;
    		if(PeekMessage(&Msg, NULL, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) && (LOWORD(Msg.lParam)==(MOD_CONTROL)) && (HIWORD(Msg.lParam)== 'Q'))
    		{
    			cout << "Key has been pressed" << endl;
    			return 0;
    		}
    				
    	}
    
    	return 0;
    
    }
    I took this from a Windows programming example and changed it slightly to work in a console application.
    I was wondering if there is a way to simplify the if statement here:
    Code:
    MSG Msg;
    if(PeekMessage(&Msg, NULL, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) && (LOWORD(Msg.lParam)==(MOD_CONTROL)) && (HIWORD(Msg.lParam)== 'Q'))
    {
    	cout << "Key has been pressed" << endl;
    	return 0;
    }
    Edit:
    Another question:
    So, once ctrl + q is pressed, i want to read and store a string the user types, while still not in the console window (minimized). Then, when the user hits a certain key combination again (maybe ctrl + w), the string will stop being read in.
    Basically, like cin >> text, except when i'm not in the console window.
    Same for outputting the text when a certain key combination is pressed.

    Thanks.
    Last edited by Cpro; 08-01-2008 at 01:15 PM.
    IDE - Visual Studio 2005
    Windows XP Pro

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    a) This belongs on the Windows forum.
    b) Without typing *into* something (i.e. console window, or even just a popup window), you'll probably have to look into "keyboard hooks". I'd guess that they're far more trouble than is worth your time, and people may also be less willing to help, since they're often used for illegitimate purposes. Alternatively, maybe you can make a quick popup window appear (like the Run dialog, for example) to input the text.

    **EDIT: I'm not sure how you'd go about 'playing back' the string. Sorry. The overall idea sounds useful though
    Last edited by Hunter2; 08-01-2008 at 01:49 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    For what you sound like you want to do, it might be really hard (or not possible, not sure) to do in a console window. If you want the console to read in a string (assuming constantly since you said till another hotkey is pressed that stops it) then the user would have to hold down the hotkey combination and type the string. It sounds like it'd be easier to do with API/dialog window vs. a console program. Making a simple window and controlling message loops is not difficult, but if you are still on console programming then explore at your own will. winprog.net has a lot of good starting tutorials.

  8. #8
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Thank you all for the feedback. I looked into keyboard hooks, and it looks to be a little over my head. So, it looks like I'll have to study up on the windows programming.

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21