Disabling paste in console application - c#

So, here's a weird question: Is there a way for me to disable the menu you get when pressing the "C:\" button at the top left of the console?
I am making a game, for fun, but it would take away alot of the fun if players were able to paste words into the command-line.
I haven't found any topics about it, so I was wondering if anyone has an idea on how to get this done, or atleast a work-around.
Thanks in advance!
Edit: Would a simple reg-ex for any ^ character work? Or does the console not use ^C and ^V?

a) Use GetConsoleWindow to get window handle; use subclassing and hooks to filter mouse and keyboard events. Though this approach probably has a lot of hidden obstacles, because of Windows console implementation specifics.
b) Use timing analysis. Measure time interval between keystrokes and ignore input if this interval is unreasonably short. You may have to experiment to find the criterion that works best. Maybe it's better to measure input duration of entrie word, not inter-character intervals.
Note that pasting text to Windows console is implemented via keyboard input simulation, i.e. when you paste something to a console, the console application observes a sequence of key-down/key-up events.

I'm not sure if the console supports pasting via a keyboard combo, you can paste either via the menu or just right clicking in the console (this depends on how the console is set up though).
I'd suggest that a better solution would be to create a winforms app instead where you can control the input/output easier than in a console app.
Otherwise, one workaround might be to call Clipboard.Clear every 100ms or so to make sure that they'll never have time to copy and paste anything until you've cleared it out, but this could of course upset and/or confuse the users if they have your application running in the background and are trying to do some work in some other software.

Related

Send keystrokes to other applications

I'm trying to write a program in C# that lets me send a keystroke to another program that's open but is not in the foreground. The purpose of the program is to let me start recording gameplay footage from my Xbox 360 using TotalMedia Extreme while also starting recording in audacity to record from the PC mic. That way I can get both tracks to be synced instead of starting gameplay recording and quickly switching to audacity and clicking record.
Basically all I need this program to do is detect a mouse click while TotalMedia Extreme is in the foreground and when the mouse is clicked, send the hotkey command to Audacity to start recording. Does anyone have an idea for how I could get started with this?
This seems like it might be a good place to start, but I don't think it will do the same thing I need to do:
Send fast textinput to another process (Window)
Check this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
I suppose you could do it through Windows Hooks, in particular WH_KEYBOARD_LL
Just use AutoHotKey. The command SendMessage will send keystrokes to a non-active window, but you could also just have AHK activate the right window before sending keystrokes. Once you are done you can wrap it into a handy little hotkey or even compile it into an exe file.
AutoHotKey is definitely the rout I'd go. You can use a function called ControlSend to send keystrokes to specific controls of specific applications, even if they're in the background. Sounds like it would work perfectly for what you're needing. The forums over at AHKScript.org are really helpful too, so you might check them out as well!

In Windows how to get highlighted text in a window that belongs to another process using C++ or C#?

It's in a C++ translation program. When user selects some text in some other program without pressing CTRL-C, I want to get the highlighted text so that I could translate it. C# solutions are welcome because this(text acquisition) is a relatively standalone component.
One possible solution is to add hook to mouse event. I detect the following messages: mouse drag with left button hold, then release left button. Then I send CTRL-C to the program and get the text from clipboard.
I save and restore clipboard so most programs would work well with the method. But I have observed some programs would process events like WM_KEYUP in their message loops. For these programs I send fake messages! Is there another safer way to get the highlighted text?
I was trying to find some API call to get text between two mouse cursor positions, but I cannot find one.
The right way to write a Windows program that interacts with another programs GUI interface is to use UI Automation. Specifically, you'll write a UI Automation Client.
It requires a bit of work, but a UI Automation solution will work with almost every other application. A hacky solution built on hooks and messages will also be a lot of work, but it will be very fragile. It will only work with certain kinds of applications in certain contexts. Lots of things can go wrong.

How do I reliably invoke clipboard copy/paste actions in other apps without messing with input queue

A while ago I wrote a simple app, which surrounds selected text in any input field in any application with some unicode symbols when user presses some hotkey. Basically, app's logic is as follows:
Register global hotkey.
Hotkey fired, now set clipboard monitor and invoke clipboard copy to see if some text was selected.
If clipboard has changed and now contains text, surround said text with symbols and then invoke clipboard paste, so input field will be updated with modified text.
The problem is, I can't get copy/paste functionality from other apps in a reliable way. What I have tried to date:
If I send WM_COPY/WM_PASTE, it's more often ignored than not, depending on the application.
If I use SendInput, keybd_event or any other keyboard-messing stuff to press/unpress usual clipboard hotkeys, it will often tamper with user-pressed keys: copy/paste uses control or shift, which are also quite popular for all generic hotkeys in all apps, my app included.
If I use Journal Hook to directly inject keyboard messages into system input queue, sometimes it will work fine, and sometimes weird glitches will occur. Also, other applications may be using JournalHook for themselves, and that will mess my app badly. Not to mention that default security policies make journal hook hard to use.
I've been trying to get/set text in the input fields using Windows UI automation instead of clipboard magic, but it rarely works.
So, if you know other ways to make other applications reliably use copy/paste functionality, or can even devise entirely different approach, I'd really appreciate you telling me :)
I think your approach isn't that bad, maybe you can start with WM_DRAWCLIPBOARD so you can monitor the clipboard. A nice sample code can be found here.
Next I'd take a look at SendKeys class - Attention: Flush, Buffer, etc. Use this to send ctrl+c/v instead of a windows message! You should get a notification from your monitor if it worked.
Now I'd use the Cliboard class to maniplulate the Data and paste it back.
Oh, it seems that I could only find more or less reliable way of invoking copy-paste without SendInput or journal hook, but I have to deal with it :-) So here it is, in case someone find it useful:
Remember and reset keyboard modifiers via AttachThreadInput(yourAppThreadId, targetAppThreadId) + SetKeyboardState(keyStateWithoutKeyboardModifiers).
Set Ctrl key modifier via SetKeyboardState(keyStateWithControl) for Ctrl+C/V hotkey.
Then PostMessage to focused control handle with WM_KEYDOWN message for C or V key, whether you want it to copy or paste.
Call Thread.CurrentThread.Join(20) to let other app process messages — and that's the most retarded moment in the whole deal, since I couldn't find the sure way to know when other app will have empty message queue.
Restore remembered keyboard modifiers.
Also, every time you do SetKeyboardState, call SetForegroundWindow(focusedControlHandle) and SetFocus(focusedControlHandle) afterwards.

calling the user attention outside of app

I want to create an alarm app for myself. On certain conditions (i need to poll websites) i want my app to inform me and make it HARD TO MISS. So i can take appropriate action or ignore it if i need to do something else.
I wrote a test app and using a BalloonTip (ShowBalloonTip with notifyIcon) isnt great. One of my previous apps brings the window in front of you and does a MessageBox however that doesnt always work or work well (if i somehow miss it or accidentally forget to click ok no futher messages will occur).
What are your suggestions?
You could also make it a system tray application and change the icon out if there is something which requires attention, a la a messenger application. That may not be "hard to miss" but I am trained (for better or worse) to look down at the icon tray when I see something blinking.
Where I work, we have a TimeTracker application (built in house) with which developers are supposed to log what we worked on and when. I am notorious for not using it. So, I wrote my own (Windows Forms) version for my own use which, every hour opens up and takes over my screen:
It is a frameless dialog which consumes the entire screen.
TopMost = True.
On resize, it sets WindowsState = Normal and resizes to fill the screen.
While it is open, it polls for taskmgr.exe and procexp.exe and kills them if found.
It disables the start menu to prevent cmd.exe commands from the menu in Windows 7.
The only way to close it is to enter a log, only then is the OK button shown!
So far, it's working out well - no one has been able to break it!
My less drastic suggestion would be to have a notification which pops up momentarily above the system tray. After a second or two, fade it out. Keep showing the notification every 30 seconds or so until it is dismissed.
Always-on-top window in the corner of the screen?
You could always set your window to be a top most window, make it full screen, and activate it. It would be very, very hard to miss...
Granted, it would also be very annoying, and not something I'd do to other users...
My "real" suggestion would be to use sound along with standard notification methods if this is going to be used by other people, as that's an easy way to grab attention without necessarily killing their workflow. A modeless window that appears in a corner of the screen, especially if combined with sound and color, can be very effective to grab attention.
The industry has been adopting these ambient orb devices and variations of it when such a hard-to-miss notification is required. It is used for tracking the stock-market and for broken-daily-builds.
http://www.ambientdevices.com/cat/orb/MAN_Ambient%20Orb_3-23-03.pdf
Regards

Windows Terminal - Windows Form to embed cmd

I'm looking for a way to embed a cmd "Shell" into a Form. I want to build a C# based application that acts as a Terminal, better than the Powershell window (no tabs) or cmd (no nothing). Just start these interpreters in the backend.
I guess MS never thought of doing this. Any ideas what From elements I could use?
Thanks,
i/o
That's not a trivial task you're undertaking. I know of one project (Console2) which basically polls the screen buffer of the underlying console window and displays in its own. You certainly will have trouble coping with interactive applications like Far and the like as they (a) rely on getting keyboard events and (b) on manipulating their screen buffer. Both are icky things if you want a suitable wrapper around the console window functionality. Mouse input is possible as well (unless Quick Edit mode is enabled) which could give you further headaches.
I doubt you can use a ready-made control for this. Basically you need to display a grid of cells each of which has a foreground and background color. You could probably use a RichTextBox for this but I'd guess it's far from ideal.
Also I don't think no one at MS ever thought of this. It's just that there's a limited budget for new features and every one of them needs to be specified, implemented, tested, tested more for regressions with millions of applications out there, etc. It's just a freaking expensive thing (if you don't want to misuse your customers as testers, which they aren't).
It would propably be the easiest to extend the Textbox class and add logic so that it behaves like a console (respond to the KeyPressed/KeyUp/KeyDown events or similar). You can also add events for those things that your console needs to respond to. For example, add a CommandEntered event.
Basing your new console on a TextBox gives you the editing and display features of the textbox "for free", so you do not need to re-implement that.
You could use a richtext box, and set the background to black and foreground to white. RTB instead of text box to handle larger amounts of data.
You would have to write an awful lot of code to simulate the terminal though.

Categories