C# replace certain string when typing in any application - c#

I want to make an application that will replace a certain string when I type in any textfield of any application (online textboxes, notepad, word, email, etc..)
For example if I'm writing in notepad++ and I type [for] and press space or enter I want my C# application to work in background, access the field and replace that string with a predefined string in my C# code.
The result would for example be:
for($i = 0; $i < X; $i++)
{
// ....
}
For example if I'm writing a word document and I input [FIRSTPAGE] I would want that to be replaced with a random string I setup early.
Later on I will setup an app that will let me change these on the fly.
I tried searching google but I found no information on anything similar.
I just need to find a way to replace a string in any textfield.
Hope this makes sense. Thanks for your help.

Create a global keyboard hook, C# : Keyboard Hook shows how you can do it.
Once hook is created monitor handle the keylog for typed word. Once the typed word is found use SendKeys to send keystrokes virtually.

There's a really cool library called Scintilla.NET - http://scintillanet.codeplex.com/
It's usually used for building your own code editor with syntax highlighting support.
But it has the auto-suggest feature you are after, i.e:

Related

Word - Replace text by hyperlinks

I am working on a MS-Word addin that reads the content of a document and replaces every occurence of a specific word by a hyperlink.
So far, I came up with this working algorithm.
// Initializes the Find parameters
searchRange.Find.ClearFormatting();
searchRange.Find.Forward = true;
searchRange.Find.Text = "foo";
do
{
searchRange.Find.Execute(Wrap: Word.WdFindWrap.wdFindStop);
if (searchRange.Find.Found)
{
// Creates a Hyperlink at the found location in the current document
this.WordDocument.Hyperlinks.Add(searchRange, externalLink, link, "bar");
}
searchRange.Find.Execute(Wrap: Word.WdFindWrap.wdFindStop);
} while (searchRange.Find.Found);
This code works, however, it can be slow on bigger documents. Thus, instead of adding hyperlinks one by one, I wanted to simply to use the Find.Replacement object and with the WdReplace.ReplaceAllproperty.
However, I cannot manage to replace my search result by a Hyperlink.
Is there a way to replace a piece of text by a hyperlink using the Replacemethod ?
In other words, I'd like to find a way to do this :
Find.Replacement.Text = new Hyperlink(...);
On an other side, I've seen that, by hitting Alt + F9in Word, we can see hyperlinks as code.
The code looks like this :
{ HYPERLINK \l "link" \o "Caption" }
Another solution would be to be able to set the text replacement as that string and make Word interpret it and thus, create the link.
Thanks for reading.
As far as I know, fields can only be inserted programmatically, or by using CTRL-F9. There are two possible reasons for this that I see:
They are not simple text. They have two ranges, the Code and the Result, only one of which is displayed at any time.
How else would a user insert text that looks like a code but is not supposed to be one, unless there was a special mechanism to create one?

Insert text at cursor position

I need to insert text from a variable at the cursor position no matter in which window/program it is located, so whenever the C# or VBA (preferably C#) code will run it will just write that text from the variable.
PD: im using VoiceBot to create custom scripts, C# default script looks like this:
using System;
using System.Drawing;
public static class VoiceBotScript
{
public static void Run(IntPtr windowHandle)
{
var myText = "This is a simple text";
//how to add this text variable to cursor position?
}
}
To clarify: VoiceBot can run C# or Visual Basic scripts on voice commands, after writing the script it will be triggered by voice. https://www.voicebot.net/ I need to run this script for example when playing a game and chat is selected, to warn player about X.
You can use SendKeys to simulate the keyboard and send keystrokes to the currently active application. For example:
SendKeys.Send("+This is a simple text");
Note that as you are simulating the keyboard, you need to explicitly invoke the Shift key (with the + character, as in the example) to get an uppercase character. There are some other caveats including other characters you have to escape, so do not simply feed this method a user-provided string without processing it first. For more information, see the documentation.

How to make Console.Clear clear all except one specific line? (c#)

How to make Console.Clear clear all except one specific line? (c#)
So, as a title says i want to clear the whole console except one specific line using c#. Is it possible?
Well, yes, it's possible. But it's not especially easy. You'll have to become familiar with the Console API, including how to call those functions with .NET. Then, what you can do:
Locate the line you want to save. If you know where it is on the screen, then it's easy to locate in the console screen buffer.
Call FillConsoleOutputCharacter to output spaces from the start of the screen buffer up to the first character of the line that you want to save.
Call FillConsoleOutputCharacter again to output spaces from the end of the line you want to save up to the end of the buffer.
I wrote a series of articles about accessing the console from C# a few years back. The articles are no longer online, but the code is available from http://mischel.com/pubs/consoledotnet.zip. You might find it useful.
yes it is possible. just use this code...
string line = yourlinenottobedeleted;
console.clear();
console.WriteLine(line);
And the deed is done
(if the line that is not to be deleted is not a string, then put int instead of string)

c#: transform a string targeted for the unix shell use for the windows command prompt

Here is a sample string I got off a socket stream.
\033[H\033[J\033[1;30HSUPERVISOR MAIN MENU\033[6;5H 0. Exit Exit\033[7;5H 1. Help Display help\033[8;5H 2. Control Calling lists and users\033[9;5H 3. Campaign Campaigns\033[10;5H 4. Manage
If you want to see the output I expect open a unix/linux shell, type echo -e followed by a space followed by the above string in single quotes and hit Enter key. The output appears something like:
SUPERVISOR MAIN MENU
0. Exit Exit
1. Help Display help
2. Control Calling lists and users
3. Campaign Campaigns
4. Manage
I want the same output except it should be in memory...I require to work with it later...any ideas.
I asked an almost equivalent question a few hours ago: Open Source C# VT100 Server. You want a client library that understands the vt100 escape commands.
I searched around for a while and to date haven't found any very good vt100 C# libraries. I've gotten started on a custom one and since I really only need to interpret left and right arrows and backspace it hasn't taken long.
Luckily the vt100 standard is very promiscuous and not overly complex. I don't think it would take you very long to whip up some code to understand the escape commands in your example. This link has a nice concise list of the VT100 escape sequences (you need to scroll down a bit). Another good site is vt100.net.
In your example the escape sequences are being in octal. Your first escape sequence is:
\033[H
which translates to the ASCII below and is used to set the cursor position.
ESC [ H
The second one is
\033[J
which translates to the ASCII sequence below and means clear the line to the end of screen.
ESC [ J

C# WPF Open File and edit certain text

So let's say I have a program with just a text box and an okay button. The user types in whatever word he wants, and when he clicks ok, it opens a specific file called Test.doc and CTRL+F for the word "test" and replaces it with whatever the user entered into the text box. How can I open said file and replace instances of the word test with the user's defined word?
Ignoring the format of the document, you could literally use the folowing for any type of file:
var contents = System.IO.File.ReadAllText(#"C:\myDoc.doc");
contents = contents.Replace("Test", "Tested");
System.IO.File.WriteAllText(#"C:\myDoc.doc", contents);
The best way would be to use the ms office interop library though.
Andrew
A number of things:
I'd recommend using a FileDialog to get the file's location. This lets you select the file to edit, but also gives you functionality to only show the file types that you want to handle in this program.
If you're handling .doc's, I'd suggest you look into VSTO and opening word docs. Here's a guide I found after a quick search. I'd suggest using it as a place to start, but you'll need to look around for more specifics.
Lastly, the string.Replace("", ""); method is probably very helpful in the CTRL-F functionality. You should be able to extract a string of the text from whatever document you're analyzing and use that method.

Categories

Resources