Results 1 to 14 of 14

Thread: WndProc for console applications??

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    WndProc for console applications??

    Hi,
    Just wondering how we can WndProc for console applications?
    I want to capture some key events that I cannot by other means.

    Thanks in advance,
    Kris

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,930

    Re: WndProc for console applications??

    How about this: https://docs.microsoft.com/en-us/dot...tframework-2.0
    I've used it on an old media player I made in 2011 but I guess it is still valid but you have to have a continues loop.
    .

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    Re: WndProc for console applications??

    As I stated in the OP: "cannot by other means" ...

    The key combinations I want to capture do not trigger the standard key events!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,920

    Re: WndProc for console applications??

    Does the Console window expose a Handle? Look at extending NativeWindow, which can have a wndProc

  5. #5

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    Re: WndProc for console applications??

    Yes ... I just tried this and it doesn't seem to work (GetConsoleWindow) below:
    VB.Net Code:
    1. Imports System.Runtime.InteropServices
    2. Imports System.Windows.Forms
    3. Public Class Module1
    4.  
    5.     Shared Sub Main()
    6.         Application.AddMessageFilter(New Intercept)
    7.  
    8.         'Console.WriteLine($"{GetConsoleWindow()}")
    9.         Console.WriteLine($"{Process.GetCurrentProcess().MainWindowHandle}")
    10.         Console.WriteLine("asd")
    11.         Console.WriteLine("asd2")
    12.         Console.ReadLine()
    13.     End Sub
    14.  
    15. End Class
    16.  
    17. Public Class Intercept
    18.     Inherits NativeWindow
    19.     Implements IMessageFilter
    20.  
    21.     <DllImport("kernel32.dll")>
    22.     Private Shared Function GetConsoleWindow() As IntPtr
    23.     End Function
    24.  
    25.     Public Sub New()
    26.         AssignHandle(GetConsoleWindow())
    27.     End Sub
    28.     Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
    29.         Debug.Print(m.ToString())
    30.         Return False
    31.     End Function
    32. End Class

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,930

    Re: WndProc for console applications??

    As this seems like a application keyboard scan , I will send you a PM because I don't want people take advantage and start scanning apps.
    .

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,920

    Re: WndProc for console applications??

    Quote Originally Posted by sapator View Post
    As this seems like a application keyboard scan , I will send you a PM because I don't want people take advantage and start scanning apps.
    @sapator - Can you also send it to me? Thanks

    @ioo - I think the problem with wndProc, is that it is designed for Windows.Forms, and the Console is a completely different type of window...

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,930

    Re: WndProc for console applications??

    Sure...
    .

  9. #9

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    Re: WndProc for console applications??

    Hrm.. I really didn't want to use keyboard hooks for two reasons:

    - I have seen virus scanners pick any keyboard hook up as a virus
    - I am doing A LOT of extending of the console and will probably require this for something else later anyway

    What I actually want it for in this specific case is to capture a copy (which this way I could probably capture from the context menus copy also somehow this way) ...
    When you have selected text, the Ctrl+C will never be passed to the Console.ReadKey etc ... Also using that would mean that it will only work when "reading" anyway ... not if it's processing!

    My goal is to copy formatted text as well as the plain text, so it can be copied with coloring etc into Word etc.

    I can get the selected area with GetConsoleSelectionInfo(), translate the points, and then use ReadConsoleOutput(), to get the text... this is all that is missing :P

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,920

    Re: WndProc for console applications??

    To monitor the clipboard for changes... This might work in a console app.

    https://www.vbforums.com/showthread....rd-for-changes

    Same problem though, as itís intended for winforms

  11. #11

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    Re: WndProc for console applications??

    That seems a bit dodge and won't work anyway...
    I need to get the selected text, and when you copy the selection has already cleared; I need the selection to get the formatted selection from the console!

    I am sure that this must be somehow possible?

  12. #12

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,394

    Re: WndProc for console applications??

    what i wrote got lost 3 time sby this forum. meh so links it is

  14. #14

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,366

    Re: WndProc for console applications??

    Are you saying that it uses a message only window and can't be captured?

    Also as stated clipboard monitoring will not work, since I need the selection area from the console, by the time it has been copied it has already been deselected!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width