Jump to content

Recommended Posts

Posted (edited)

FixGibberish

 many times i start typing without having watched the keyboard to the language i'm typing in,  and when I look to see what I've typed I see gibberish.
 Function to convert "gibberish" text (e.g., "kalhmera se oloyw") into correct text (e.g., "καλημερα σε ολους")
 and vice versa  (e.g., "Γοοδ μορνινγ εωερυονε") into correct text (e.g., "Good morning everyone")

(you will need to adapt it to your language)  😁
 
FixGibberish_simple.au3    (1:1 conversions)

Spoiler
; https://www.autoitscript.com/forum/topic/213301-fixgibberish/
;----------------------------------------------------------------------------------------
; Title...........: FixGibberish_simple.au3
; Description.....: Convert the currently selected text from gibberish to the appropriate language
; AutoIt Version..: 3.3.18.0   Author: ioa747           Script Version: 0.1
; Note............: Testet in Windows 11 Pro 24H2       Date:4/11/2025
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

; Hotkey to trigger the conversion
HotKeySet("+^q", "FixGibberish") ; <<--(Shift+Ctrl+Q)--<<


; Main loop to keep the script running
While 1
    Sleep(300)
WEnd

;--------------------------------------------------------------------------------------------------------------------------------
; many times i start typing without having watched the keyboard to the language i'm typing in,
; and when I look to see what I've typed I see gibberish.
; Function to convert "gibberish" text (e.g., "kalhmera se oloyw") into correct text (e.g., "καλημερα σε ολους")
; and vice versa  (e.g., "Γοοδ μορνινγ εωερυονε") into correct text (e.g., "Good morning everyone")
;--------------------------------------------------------------------------------------------------------------------------------
Func FixGibberish()

    Local $hWnd = WinGetHandle("[ACTIVE]") ; Get the handle of the active window
    Local Static $mEN[]
    Local Static $mGR[]

    If UBound($mEN) = 0 Then
        Local Const $sEN = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
        Local Const $sGR = ";ςερτυθιοπασδφγηξκλζχψωβνμ:΅ΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜ"
        For $i = 1 To StringLen($sEN)
            $mEN[StringMid($sGR, $i, 1)] = StringMid($sEN, $i, 1)
            $mGR[StringMid($sEN, $i, 1)] = StringMid($sGR, $i, 1)
        Next
    EndIf

    ; Local $hTimer = TimerInit()
    Local Const $CHANGE_THE_KEYBOARD = 1  ; constant if you want to send key for change the keyboard too
    Local Const $iTimeout = 1000          ; Max wait time for clipboard (1 second)
    Local $sOriginalClipboard = ClipGet() ; Backup original clipboard content
    Local $sInputText = ""

    ClipPut("") ; Clear clipboard to detect new content
    Send("{CTRLDOWN}")
    Send("c")   ; Copy selected text to clipboard
    Send("{CTRLUP}")

    ; Wait for clipboard content to be updated
    Local $iStartTime = TimerInit()
    While TimerDiff($iStartTime) < $iTimeout
        $sInputText = ClipGet()
        If $sInputText <> "" Then ; Check if new content arrived
            ExitLoop
        EndIf
        Sleep(50)
    WEnd

    If $sInputText = "" Then
        ConsoleWrite("! No text copied to clipboard or clipboard timed out." & @CRLF)
        ClipPut($sOriginalClipboard) ; Restore clipboard
        Return SetError(1, 0, 0)
    EndIf

    ; Determine conversion direction (English to Greek or Greek to English)
    ; check if the input text contains English or Greek characters
    Local $sChar, $bEnglishTxt = False
    $sChar = StringLeft($sInputText, 1)
    If Not MapExists($mEN, $sChar) Then $bEnglishTxt = True

    Local $sOutputText = ""

    If $bEnglishTxt Then
        ; converted to Greek
        For $i = 1 To StringLen($sInputText)
            $sChar = StringMid($sInputText, $i, 1)
            If MapExists($mGR, $sChar) Then
                $sOutputText &= $mGR[$sChar]
            Else
                $sOutputText &= $sChar
            EndIf
        Next
    Else
        ; converted to English
        For $i = 1 To StringLen($sInputText)
            $sChar = StringMid($sInputText, $i, 1)
            If MapExists($mEN, $sChar) Then
                $sOutputText &= $mEN[$sChar]
            Else
                $sOutputText &= $sChar
            EndIf
        Next
    EndIf

    ClipPut($sOutputText) ; Put the converted text back to clipboard
    WinActivate($hWnd)    ; Re-activate the window where text was copied from
    Sleep(50)             ; Give a small delay

    ; Paste the converted text
    Send("{CTRLDOWN}")
    Send("v")
    Send("{CTRLUP}")

    If $CHANGE_THE_KEYBOARD Then Send("# ") ; Win + space, if you want to change the keyboard
    ClipPut($sOriginalClipboard)            ; Restore the original clipboard content
    ; ConsoleWrite("Original Clipboard Restored. Processed in: " & Round(TimerDiff($hTimer) / 1000, 3) & " seconds" & @CRLF)
EndFunc   ;==>FixGibberish

 

 


To the new approach,
I also added the logic of "dead keys", where the accented characters are a combination of 2 keys, a dead key and a vowel.

FixGibberish.au3    (Dead Key + Vowel) conversions

; https://www.autoitscript.com/forum/topic/213301-fixgibberish/
;----------------------------------------------------------------------------------------
; Title...........: FixGibberish.au3
; Description.....: Convert the currently selected text from gibberish to the appropriate language
; AutoIt Version..: 3.3.18.0   Author: ioa747           Script Version: 0.2
; Note............: Testet in Windows 11 Pro 24H2       Date:11/11/2025
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

; Hotkey to trigger the conversion
HotKeySet("+^q", "FixGibberish") ; <<--(Shift+Ctrl+Q)--<<
HotKeySet("!^x", "_ExitScript")  ; <<--(Alt+Ctrl+X)--<<

; Main loop to keep the script running
While 1
    Sleep(300)
WEnd

;--------------------------------------------------------------------------------------------------------------------------------
Func _ExitScript()
    Exit
EndFunc   ;==>_ExitScript

;--------------------------------------------------------------------------------------------------------------------------------
; Function to convert keyboard layout
;--------------------------------------------------------------------------------------------------------------------------------
Func FixGibberish()
    ; Define maps as static for single-time initialization
    Local Static $mEN[]          ; Greek -> English (1:1 or 1:2 for dead keys)
    Local Static $mGR[]          ; English -> Greek (1:1)
    Local Static $mDeadKeyGR[]   ; English 2-char sequence -> Greek 1-char output

    ; ⚠️ List of all possible English characters that are Dead Keys.
    Local Static $sEnglishDeadKeys = ";:W"

    If UBound($mEN) = 0 Then
        ; -------------------------------------------------------
        ; ### STANDARD QWERTY MAPPING (1:1 conversions) ###
        ; -------------------------------------------------------
        Local Const $sEN_Q = "qwertyuiopasdfghjklzxcvbnmQERTYUIOPASDFGHJKLZXCVBNM"
        Local Const $sGR_Q = ";ςερτυθιοπασδφγηξκλζχψωβνμ:ΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜ"

        For $j = 1 To StringLen($sEN_Q)
            $mEN[StringMid($sGR_Q, $j, 1)] = StringMid($sEN_Q, $j, 1) ; Greek to English map (Base 1:1)
            $mGR[StringMid($sEN_Q, $j, 1)] = StringMid($sGR_Q, $j, 1) ; English to Greek map (Base 1:1)
        Next

        ; -------------------------------------------------------
        ; ### INITIALIZE DEAD KEY MAPS (Dead Key + Vowel) ###
        ; -------------------------------------------------------
        Local Const $aDeadKeyMappings[][] = [ _
                [";a", "ά"], [";e", "έ"], [";i", "ί"], [";o", "ό"], [";y", "ύ"], [";h", "ή"], [";v", "ώ"], _ ; TONOS (; + Vowel)
                [";A", "Ά"], [";E", "Έ"], [";I", "Ί"], [";O", "Ό"], [";Y", "Ύ"], [";H", "Ή"], [";V", "Ώ"], _ ; TONOS (; + Vowel)
                [":i", "ϊ"], [":y", "ϋ"], [":I", "Ϊ"], [":Y", "Ϋ"], _ ; DIALYTIKA (: + Vowel)
                ["Wi", "ΐ"], ["Wy", "ΰ"] _  ; DIALYTIKA + TONOS (W + Vowel)
                ]

        For $j = 0 To UBound($aDeadKeyMappings) - 1
            Local $sENKey = $aDeadKeyMappings[$j][0]
            Local $sGRValue = $aDeadKeyMappings[$j][1]

            $mDeadKeyGR[$sENKey] = $sGRValue ; Forward Map (e.g. "Wi" -> "ΐ")
            $mEN[$sGRValue] = $sENKey        ; Reverse Map (e.g. "ΐ" -> "Wi")
        Next

        ; Reverse mapping for the single Dead Key characters themselves (if they appear alone)
        $mEN["΄"] = ";"   ; TONOS
        $mEN["¨"] = ":"   ; DIALYTIKA
        $mEN["΅"] = "W"    ; DIALYTIKA + TONOS
        ; ...
    EndIf

    Local $hWnd = WinGetHandle("[ACTIVE]") ; Get the handle of the active window
    Local Const $CHANGE_THE_KEYBOARD = 1   ; constant if you want to send key for change the keyboard too
    Local Const $iTimeout = 1000           ; Max wait time for clipboard (1 second)
    Local $sOriginalClipboard = ClipGet()  ; Backup original clipboard content
    Local $sInputText = ""

    ClipPut("") ; Clear clipboard to detect new content
    Send("{CTRLDOWN}")
    Send("c")   ; Copy selected text to clipboard
    Send("{CTRLUP}")

    ; Wait for clipboard content to be updated
    Local $iStartTime = TimerInit()
    While TimerDiff($iStartTime) < $iTimeout
        $sInputText = ClipGet()
        If $sInputText <> "" Then
            ExitLoop
        EndIf
        Sleep(50)
    WEnd

    If $sInputText = "" Then
        ConsoleWrite("! No text copied to clipboard or clipboard timed out." & @CRLF)
        ClipPut($sOriginalClipboard) ; Restore clipboard
        Return SetError(1, 0, 0)
    EndIf

    ; Determine conversion direction
    Local $sChar, $bEnglishTxt = False
    $sChar = StringLeft($sInputText, 1)
    If Not MapExists($mEN, $sChar) Then $bEnglishTxt = True

    Local $sOutputText = ""

    If $bEnglishTxt Then
        ; Convert TO Greek (Dead Key Logic)
        Local $i = 1
        While $i <= StringLen($sInputText)
            $sChar = StringMid($sInputText, $i, 1)

            ; Look for Dead Key sequence only if we haven't reached the end
            If StringInStr($sEnglishDeadKeys, $sChar) > 0 And $i < StringLen($sInputText) Then

                Local $sNextChar = StringMid($sInputText, $i + 1, 1)
                Local $sDeadKeySequence = $sChar & $sNextChar

                If MapExists($mDeadKeyGR, $sDeadKeySequence) Then
                    ; Found a match (e.g. ;i -> ί)
                    $sOutputText &= $mDeadKeyGR[$sDeadKeySequence]
                    $i += 2 ; Advance by two characters
                    ContinueLoop
                EndIf
                ; Fallthrough: Dead key not followed by a valid vowel (e.g. ;z).
            EndIf

            ; Normal 1:1 conversion (for letters, non-dead key symbols, or invalid dead key sequence)
            If MapExists($mGR, $sChar) Then
                $sOutputText &= $mGR[$sChar]
            Else
                $sOutputText &= $sChar
            EndIf
            $i += 1
        WEnd

    Else
        ; Convert TO English (Reverse 1:1 conversion)
        For $i = 1 To StringLen($sInputText)
            $sChar = StringMid($sInputText, $i, 1)
            If MapExists($mEN, $sChar) Then
                ; This handles both 1:1 (e.g. "ς" -> "w") AND 1:2 (e.g. "ΐ" -> "Wi")
                $sOutputText &= $mEN[$sChar]
            Else
                $sOutputText &= $sChar
            EndIf
        Next
    EndIf

    ClipPut($sOutputText)
    WinActivate($hWnd)
    Sleep(50)

    ; Paste the converted text
    Send("{CTRLDOWN}")
    Send("v")
    Send("{CTRLUP}")

    If $CHANGE_THE_KEYBOARD Then Send("# ")
    ClipPut($sOriginalClipboard)
    Return 0
EndFunc   ;==>FixGibberish

 

Please, every comment is appreciated!
leave your comments and experiences here!
Thank you very much  :)

 

Edited by ioa747
more appropriate name + (Dead Key + Vowel) conversions

I know that I know nothing

Posted

The name you choose is a misnomer. "proper case" refers to capitalizing the first letter of each word in a sentence, like this: "Proper Case" Refers To Capitalizing The First Letter Of Each Word In A Sentence

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • ioa747 changed the title to _ConvertKeyboardLayout
  • ioa747 changed the title to FixGibberish
Posted

To the new approach,  (I updated the first post.)
I also added the logic of "dead keys", where the accented characters are a combination of 2 keys, a dead key and a vowel.

I know that I know nothing

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...