Keyboard Focus

This is a discussion on Keyboard Focus within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I was wondering... How can you give the desktop or taskbar the keyboard focus?...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy Keyboard Focus?

    Hi all,

    I was wondering...
    How can you give the desktop or taskbar the keyboard focus?
    Last edited by Aidman; 01-05-2003 at 10:27 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    I know there is an SetFocus function but that only works for a window in your own app.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can try...
    Code:
    SendMessage( GetDesktopWindow(),WM_SETFOCUS,(WPARAM)hwnd_of_your_window_thats_losing_focus,0);
    That would be my first try.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    re: reply

    the SendMessage didn't work, the focus didn't change.
    The return value is zero and acording to MSDN that should
    mean that the message was successfully completed...

    Does any one know how to give the desktop the keyboard focus?

    I know there is an SetFocus function but that only works on windows in you own app. But accordning to MSDN you could also
    use the AttachThreadInput function and attach your calling thread to the desktop thread but I don't exactly understand how to do it and wonder if anyone could demonstrate it in code or explain another alternetive...
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    maybe setfocus was the wrong message. Try WM_ACTIVATE. You will need to check what to pack into wparam and lparam. Im not sure how to do this never having tried. You could alternatively try some of the window functions using the window handle you get from GetDesktopWindow() or if you are lucky someone else here may know how to do it. Ken Fitlike,AdrianXW and Fordy would be the people to ask.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Keyboard Focus?

    Originally posted by Aidman
    Hi all,

    I was wondering...
    How can you give the desktop or taskbar the keyboard focus?
    I dont see what you are trying to do......if you give keyboard focus to either of these, there's very little you can do as a user.....

    Say whay you are really trying to achive and someone might give a better answer.....

    Also, you may find that simulating windows messages like WM_SETFOCUS etc....might work better if you call SetForegroundWindow(GetDesktopWindow()) as the window thread wont have the thread priority to work with the keyboard when it doesnt sit at the top of the Z order....even better, minimise all active windows and then the desktop will be able to recieve input by default....

    Code:
    #include <windows.h>
    
    BOOL CALLBACK Minimise(HWND hWnd,LPARAM){
    
    	static WINDOWPLACEMENT wp;
    	
    	if(GetWindowLong(hWnd,GWL_STYLE) & WS_VISIBLE 
    		&& GetWindowPlacement(hWnd,&wp)
    		&& wp.showCmd != SW_SHOWMINIMIZED)
    		ShowWindow(hWnd,SW_SHOWMINIMIZED);
    
    	return 1;
    }
    
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
    
    	EnumWindows(Minimise,0);
    
    	return 0;
    }
    Still dont understand what you are trying to do though....

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    I am making a program that hasn't a window but uses GetAsyncKeyState to get inputs. The reason for not using a window with normal VM_KEYUP/KEYDOWN is becouse the keyboard input must the in a global state. Now in a specific state in the program I don't want the other programs to get keyboard input becouse they might write it in there active textbox or do some of there shortcut key commands. And don't wanna use hotkeys becouse I need to refer to all keys that are pressed.

    If you click on your desktop with your cursor then the desktop gets the keyboard focus even if it isn't the top window. I need to set the keyboard focus to a either the desktop where it would do less key commands or the taskbar. Please help me find I way with out minimizing all windows...
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Aidman
    I am making a program that hasn't a window but uses GetAsyncKeyState to get inputs. The reason for not using a window with normal VM_KEYUP/KEYDOWN is becouse the keyboard input must the in a global state. Now in a specific state in the program I don't want the other programs to get keyboard input becouse they might write it in there active textbox or do some of there shortcut key commands. And don't wanna use hotkeys becouse I need to refer to all keys that are pressed.

    If you click on your desktop with your cursor then the desktop gets the keyboard focus even if it isn't the top window. I need to set the keyboard focus to a either the desktop where it would do less key commands or the taskbar. Please help me find I way with out minimizing all windows...
    Right...I think I see what you are doing, but I dont think you are going about it in the most practical way...

    Better to create a dll to activate a system wide Keyboard hook.....this hook procedure will then get any keypress message before the window it belongs to.....you can then alter, store, divert or discard the message as you wish.......you can also open a method of IPC (Inter Process Communication) to relay these messages yo your app (Named pipe is good....I prefer a mailslot)....

    Try look up keyloggers and the SetWindowsHook API

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Talking Thanks!

    Hey thanks this is really great! thanks Fordy!
    I think this might really help and if so then WOPPY!!!
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard focus without activation?
    By kgen in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2008, 02:25 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. I need help disabling Keyboard and Mouse input on Edit controls
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2003, 12:59 AM
  5. Focus & Always-On-Top :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 06-13-2002, 06:44 PM

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