|
My requirement is to create a transliteration application. If I do this as you suggested, its ability limits to one application. But I want allow users to do transliteration on any window. To do so first I want to avoid typing English letters and track the key which was pressed. Later it will send translated letter to the active window. That's what I want.
|
|
|
|
|
The onyl way to do that would be a global keyboard hook.
The problem is you get keyboard scancodes, not letters. You'll have to map the key to the letter you want, see if it's allowed, and if so, send the key message down to the next hook in the chain. If not, you'll have to find the letter that is allowed, map that back to a scancode, alter the keyboard message and send that down the hook chain.
|
|
|
|
|
As I know, .NET Framework doesn't support global hooks and I am not familiar with hooks . Can you suggest another possible way to do this with .NET
|
|
|
|
|
What Dave says goes; if you don't like it that's too bad.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Pasan148 wrote: As I know, .NET Framework doesn't support global hooks
Actually, you're wrong. Global Mouse and Keyboard hooks, among a couple of others, work just fine in .NET. You just have to Google for examples.
Pasan148 wrote: Can you suggest another possible way to do this with .NET
There is no other way.
|
|
|
|
|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) I used following code to implement my project as you suggest
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYUP As Integer = &H105
Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
Private hookID As IntPtr
Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
<DllImport("user32")> _
Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, _
ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
hookID = SetHook(proc)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
UnhookWindowsHookEx(hookID)
End Sub
Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
Using curProcess As Process = Process.GetCurrentProcess()
Using curModule As ProcessModule = curProcess.MainModule
Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
End Using
End Using
End Function
But there is a problem in this function
Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
If nCode Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
If vkCode = Keys.A Then
SendKeys.Send("B")
Return CType(1, IntPtr)
End If
End If
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End Function
If someone press 'A', I want to display 'B'. But according to this function it displays 'B' twice. How can I overcome this problem.
|
|
|
|
|
It's doing that because you used SendKeys n a keyboard hook.
Seriously, read up on what you're doing with a keyboard hook. When you callback function is, well, called, you are given the keyscan code before the system gets the key. This is your opportunity to MODIFY THE MESSAGE before it gets to the system.
Remove the SendKeys line and replace it with a line that sets a new value for lParam with the appropriate key you want.
|
|
|
|
|
Please can you show me the code to set a new value for lParam variable. Since I am not familiar with hooks I can't understand how to do that.
|
|
|
|
|
All you're doing is converting the new keycode to an Integer and passing the new value to the CallNextHookEx function. Seriously, why is this so hard?
|
|
|
|
|
Pasan148 wrote: Is there a way to avoid typing alphabetic keys (A-Z) in any window and track the key which was pressed at same time.
Just as a warning; you'd be violating the law if this application would be running on a PC in the Netherlands without the users' prior consent.
I are Troll
|
|
|
|
|
When trying to transliterate the input, you should also know that the keyboard layout is quite different for each set of characters. And some languages use many more characters than does English, many write syllables (Japanese, Indian alphabets including Thai and Khmer) or words (Chinese, Japanese).
|
|
|
|
|
i need to examine a text file containing Arabic writings, by counting each character frequency of each character, how do i do this in vb.net? Do i need to detect the file code-page before reading? do i need to convert the file code-page before doing anything? AM using vb.net 2008
|
|
|
|
|
For my 2cts; I don't think there is any way to detect the codepage, you ought to know it before opening it. Text files don't have any metadata to tell you anything about its content.
So if you know the encoding, you can load the full file by using System.IO.File.ReadAllText(path As String, encoding As Encoding) As String . From here you should have a properly encoded string and you can start counting characters.
If you want something done fast, then do it right (Grissom, CSI)
Thanks for your reply, you just acknowledged my existence
|
|
|
|
|
what i want to do is count the frequency of characters that appears in an Arabic text
|
|
|
|
|
Have you tried it? Or can you be more specific?
A string in .NET is not just a list of bytes. Every character consists of 1 or more bytes depending on the encoding used.
The method provided will read the file into a proper encoded string. All you have to do is traverse the string and count the characters.
If you want something done fast, then do it right (Grissom, CSI)
Thanks for your reply, you just acknowledged my existence
|
|
|
|
|
All you have to do is read the file into a String, then iterate over the Characters in the string and add them to a Dictionary collection. Since Dictionary is a key/value pair collection, the "key" will be the character you are looking at. The "value" will be the count of those characters. When you go to add the character to the collection, you first see if it is already there, and if so, get it's value and increment it by one. If not, add the new key with a value of 1. Move on the next character...
|
|
|
|
|
Cool Smith wrote: i need to examine a text file containing Arabic writings
What format is it in? Ideally, it'd be UTF. It's important since the encoding determines the length of a single character. Download a HEX-editor and open the textfile with it - what do the first bytes look like in HEX?
Cool Smith wrote: Do i need to detect the file code-page before reading?
There's no way of detecting it with good precision, but Notepad can tale an educated guess[^]. If you have any say in it, then it should be UTF. If you don't, ask which codepage was used to write the files. There'll be a difference in Windows Arabic 1256[^] and DOS Arabic 864[^]
Cool Smith wrote: by counting each character frequency of each character, how do i do this in vb.net?
First, determine the encoding, and read the file with that encoding. Then create a dictionary, read the entire file as a string. Loop through the string by eating characters, adding them to the dictionary as the key, or adding +1 to it's value if it's already in the dictionary. When done eating, burp out the results
I are Troll
|
|
|
|
|
What format is it in? Ideally, it'd be UTF. It's important since the encoding determines the length of a single character. Download a HEX-editor and open the textfile with it - what do the first bytes look like in HEX?
The this is, the software will be examining different text files (*.txt) only that contains arabic writings. i found code here that can detect the code page of a file and another that can convert between different code page.
First, determine the encoding, and read the file with that encoding. Then create a dictionary, read the entire file as a string. Loop through the string by eating characters, adding them to the dictionary as the key, or adding +1 to it's value if it's already in the dictionary. When done eating, burp out the results
can you give me pseudo code for this, i don't have any idea how to do it
|
|
|
|
|
Cool Smith wrote: i found code here that can detect the code page of a file
Can you post a link to that article? I haven't read it yet
Cool Smith wrote: can you give me pseudo code for this It'd go something like this;
Dictionary<String, Int64> characterCounter = new Dictionary<String, Int64>();
string theFile = File.ReadAllText("C:\test.txt");
while (theFile.Length > 0)
{
string CurrentCharacter = theFile[theFile.Length -1];
string theFile = theFile.Remove(theFile.Length -1, 1);
if (characterCounter.ContainsKey(CurrentCharacter))
{
characterCounter[CurrentCharacter] = characterCounter[CurrentCharacter] + 1;
}
else
{
characterCounter.Add(CurrentCharacter, 1);
}
}
for each (DictionaryEntry<String, Int64> entry in characterCounter)
{
textBox1.Text += String.Format("char {0} occurs {1} times", entry.Key, entry.Value);
} This could be a bit slow with large files, as it forces .NET to allocate memory each time for a new string. It'd be more efficient if it were a moving frame. That'd go something more like this;
string theFile = File.ReadAllText("C:\test.txt");
Int64 currentPos = 0;
Int64 endPos = theFile.Length -1;
while (currentPos <> endPos)
{
string CurrentCharacter = theFile[currentPos];
currentPos = currentPos + 1;
...
}
I are Troll
|
|
|
|
|
here are the links
CodePage File Converter[^]
Detect Encoding for In- and Outgoing Text[^]
i'll try your implementation and and back to you.
besides i found a hextostring code, will it work well for recognizing single characters in a joined character
<br />
Private Function ConvertStringToHex(ByVal MyString As String) As String<br />
Dim Result As String = vbNullString<br />
If Len(MyString) = 0 Then<br />
Result = vbNullString<br />
Else<br />
For i As Integer = 0 To Len(MyString.Trim) - 1<br />
Dim MyChar As String = Mid(MyString.Trim, i + 1, 1)<br />
Result = Result + Xformat(Hex(Microsoft.VisualBasic.AscW(MyChar)))<br />
Next<br />
End If<br />
Return Result<br />
End Function<br />
Private Function ConvertHexToString(ByVal MyString As String) As String<br />
<br />
Dim Result As String = vbNullString<br />
If Len(MyString) = 0 Then<br />
Result = vbNullString<br />
Else<br />
For i As Integer = 0 To Len(MyString.Trim) - 1 Step 4<br />
Dim MyChar As String = Mid(MyString.Trim, i + 1, 4)<br />
Result = Result + Microsoft.VisualBasic.ChrW(Convert.ToInt32(MyChar, 16))<br />
Next<br />
End If<br />
Return Result<br />
<br />
<br />
End Function<br />
Function Xformat(ByVal xin As String) As String<br />
Dim retval As String = xin<br />
Select Case Len(xin)<br />
Case Is = 3<br />
retval = "0" & xin<br />
Case Is = 2<br />
retval = "00" & xin<br />
Case Is = 1<br />
retval = "000" & xin<br />
End Select<br />
Return retval<br />
End Function<br />
End Class<br />
<br />
|
|
|
|
|
|
Cool Smith wrote: first am using vb.net not c#, i tried convertin to vb.net
You asked for pseudocode, and that's what it is.
Cool Smith wrote: Can you provide vb.net version?
No, since it's not my job. You could post your code however, and people could have a look. That is, if you explain where you're stuck.
I are Troll
|
|
|
|
|
How do you store/retrieve a PDF file in SQL Server using vb.net code?
|
|
|
|
|
Have a read of this article there is a lnk in the article that has a code example to do what your after
loading binary data (files) in a database [^]
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
I am working on a library that uses multiple threads to do all kinds of work. The work being done results in events being generated. Preferably I would like all communications to the outside world to be on a single thread, so most threading issues can be dealt with inside the library.
When using forms there are the standard components with the Invoke methods that let the thread that runs the form call the requested Delegate , so whatever needs to be done (raise an event in my case) gets executed on that form thread.
Is there a way to do this generically, without forms or components ? Say I have 1 main thread that spawns 10 or 20 worker threads. And whenever the workers have an event to raise, they let the main thread do this? Is there an easy way (that I just haven't found), or should I be creating some custom threadpool management code to achieve this?
Any thoughts?
If you want something done fast, then do it right (Grissom, CSI)
Thanks for your reply, you just acknowledged my existence
|
|
|
|
|