Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      From Data To Decisions: UX Strategies For Real-Time Dashboards

      September 13, 2025

      Honeycomb launches AI observability suite for developers

      September 13, 2025

      Low-Code vs No-Code Platforms for Node.js: What CTOs Must Know Before Investing

      September 12, 2025

      ServiceNow unveils Zurich AI platform

      September 12, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025

      Distribution Release: Q4OS 6.1

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Optimizely Mission Control – Part III

      September 14, 2025
      Recent

      Optimizely Mission Control – Part III

      September 14, 2025

      Learning from PHP Log to File Example

      September 13, 2025

      Online EMI Calculator using PHP – Calculate Loan EMI, Interest, and Amortization Schedule

      September 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      sudo vs sudo-rs: What You Need to Know About the Rust Takeover of Classic Sudo Command

      September 14, 2025
      Recent

      sudo vs sudo-rs: What You Need to Know About the Rust Takeover of Classic Sudo Command

      September 14, 2025

      Dmitry — The Deep Magic

      September 13, 2025

      Right way to record and share our Terminal sessions

      September 13, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: The Wrong Kind of Character

    CodeSOD: The Wrong Kind of Character

    April 29, 2025

    Today’s code, at first, just looks like using literals instead of constants. Austin sends us this C#, from an older Windows Forms application:

    <span class="hljs-keyword">if</span> (e.KeyChar == (<span class="hljs-built_in">char</span>)<span class="hljs-number">4</span>) {   <span class="hljs-comment">// is it a ^D?</span>
            e.Handled = <span class="hljs-literal">true</span>;
            DoStuff();
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (e.KeyChar == (<span class="hljs-built_in">char</span>)<span class="hljs-number">7</span>) {   <span class="hljs-comment">// is it a ^g?</span>
            e.Handled = <span class="hljs-literal">true</span>;
            DoOtherStuff();
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (e.KeyChar == (<span class="hljs-built_in">char</span>)Keys.Home) {
            e.Handled = <span class="hljs-literal">true</span>;
            SpecialGoToStart();
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (e.KeyChar == (<span class="hljs-built_in">char</span>)Keys.End) {
            e.Handled = <span class="hljs-literal">true</span>;
            SpecialGoToEnd();
    } 
    

    Austin discovered this code when looking for a bug where some keyboard shortcuts didn’t work. He made some incorrect assumptions about the code- first, that they were checking for a KeyDown or KeyUp event, a pretty normal way to check for keyboard shortcuts. Under that assumption, a developer would compare the KeyEventArgs.KeyCode property against an enum- something like e.KeyCode == Keys.D && Keys.Control, for a CTRL+D. That’s clearly not what’s happening here.

    No, here, they used the KeyPressEvent, which is meant to represent the act of typing. That gives you a KeyPressEventArgs with a KeyChar property- because again, it’s meant to represent typing text not keyboard shortcuts. They used the wrong event type, as it won’t tell them about modifier keys in use, or gracefully handle the home or end keys. KeyChar is the ASCII character code of the key press: which, in this case, CTRL+D is the “end of transmit” character in ASCII (4), and CTRL+G is the goddamn bell character (7). So those two branches work.

    But home and end don’t have ASCII code points. They’re not characters that show up in text. They get key codes, which represent the physical key pressed, not the character of text. So (char)Keys.Home isn’t really a meaningful operation. But the enum is still a numeric value, so you can still turn it into a character- it just turns into a character that emphatically isn’t the home key. It’s the “$”. And Keys.End turns into a “#”.

    It wasn’t very much work for Austin to move the event handler to the correct event type, and switch to using KeyCodes, which were both more correct and more readable.

    [Advertisement]
    Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleFog Ransomware Directory With Active Directory Exploitation Tools & Scripts Uncovered
    Next Article Oshin OS is an Arch Linux distribution

    Related Posts

    News & Updates

    Building personal apps with open source and AI

    September 12, 2025
    News & Updates

    What Can We Actually Do With corner-shape?

    September 12, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-6179 – Google ChromeOS Extension Management Permissions Bypass

    Common Vulnerabilities and Exposures (CVEs)

    Streamline machine learning workflows with SkyPilot on Amazon SageMaker HyperPod

    Machine Learning

    ChatGPT Is Making People Think They’re Gods and Their Families Are Terrified

    Artificial Intelligence

    OpenAI rolls out new o3-pro AI model to ChatGPT Pro subscribers

    Operating Systems

    Highlights

    Free decryptor for victims of Phobos ransomware released

    July 24, 2025

    There is good news for any organisation which has been hit by the Phobos ransomware.…

    CVE-2025-5623 – D-Link DIR-816 Stack-Based Buffer Overflow Vulnerability

    June 4, 2025

    CVE-2025-45490 – Linksys E5600 Command Injection Vulnerability

    May 6, 2025

    CVE-2025-5328 – “Chshcms McCms Remote Path Traversal Vulnerability”

    May 29, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.