How to send char as message to another application

CK CK 3788 
Created at
Updated at  
542 0 0

The following code is used to insert a character into another application - Notepad.

procedure TForm1.Button1Click(Sender: TObject) ;
var g,d:integer;
begin
  {Find the main window of the Application}
  g:=FindWindow('Notepad',nil) ;
  {Find the window of the application's text box}
  d:=ChildWindowFromPoint(g,point(50,50)) ;
  {Now send it a character!!}
  SendMessage(d,WM_CHAR,Ord('A'),0) ;
end;


Sending a Character to Another Application (Notepad)

  • Understanding the Code: This code snippet utilizes Windows API functions to send a character ('A') to the Notepad application.
  • Key Concepts:
    • FindWindow: This function searches for a window with a specific class name ('Notepad') and window title (nil - meaning any title). It returns the handle (unique identifier) of the found window.
    • ChildWindowFromPoint: This function retrieves the handle of a child window located at a specific point (50, 50) within the parent window (Notepad). In this case, it's assumed that the point (50, 50) corresponds to the Notepad's text box area.
    • SendMessage: This function sends a message to the specified window (the text box). The message is WM_CHAR, indicating that a character needs to be inserted. The 'Ord('A')' part converts the character 'A' to its ASCII code, which is used as the message's 'wParam' parameter. The '0' is the message's 'lParam' parameter, which is not used in this case.
  • Alternative Methods:
    • SendKeys: This method offers a simpler approach to send keystrokes to other applications. It's often preferred for its ease of use, although it might not have the same level of control over the character input process.
  • Important Considerations:
    • Window Handles: It's essential to ensure the correct window handles are retrieved. Otherwise, the character might be sent to the wrong window.
    • Focus: To ensure the character is inserted correctly, the target application should be in focus (active).
    • Permissions: Running such code might require specific permissions or user privileges, depending on the target application and the operating system.
  • Further Exploration:
    • Windows API Documentation: Refer to the Microsoft documentation for the Windows API functions used in the code.
    • Automation Libraries: Explore libraries such as AutoIt, AutoHotkey, or Python's PyAutoGUI for more powerful and flexible automation tasks.
Tags AssignFile TPicture chkdsk kbdevent Facebook X
Comments 0
Similar posts
  1. How to restrict mouse movement
    407
  2. How to save text from Clipboard to a file
    566
  3. List Devices (LPT, COM ports, ...)
    477
  4. How to know whether the font is TrueTypeFont or not
    374
  5. How to track a user's idle time
    587
  6. How to remove your application from TaskBar
    655
  7. How to show window contents while dragging
    451
  8. How to use TTF Font Without Installing
    1,113
  9. Window flashing
    378
  10. How to play sounds on the PC Speaker ?
    524
  11. How to install an INF file using Delphi ?
    460
  12. How to determine the output of a console application ?
    449
  13. Get text from the control at pos x,y on screen
    465
  14. Get dimensions of usable space on monitor (work area)
    486
  15. Get system info (free memory, etc)
    404
  16. How to Get Filenames from Clipboard ?
    1,502
  17. How to Get Windows OS Version ?
    425
  18. Get Windows Temp directory
    656
  19. Disable ALT+TAB, CTRL+ESC, CTRL+ALT+DEL
    1,823
  20. Display Standard Windows Properties dialog
    1,127
  21. Disable Mouse and Keyboard from Delphi Code
    791
  22. Detecting Drive Types
    467
  23. Detecting and preventing Windows shut down
    433
  24. Copying Group of Files using Delphi with Standard Animation Dialog (SHFileOperation)
    412
  25. Controling sound volume from code
    418
  26. How to Close Another Application by Windows Caption
    386
  27. How to Change the Windows Start button bitmap
    422
  28. How to capture the output from a DOS (command/console) Window
    962
  29. How to Activate/Deactivate the Screen Saver
    856
  30. Keyboard event detection (Key Tracker)
    1,084
  1. How to get parameter string ?
    1,234
  2. Save TBitmap image to Jpeg format image in Delphi
    710
  3. How to get screen resolution in case of using multiple monitors ?
    448
  4. How to turn off monitor ?
    426
  5. Find File at certain directory in Delphi
    436
  6. Sending email messages in .Net
    1,030
  7. Delphi for .Net Code Folding keyboard shortcuts
    525
  8. How to encrypt a string
    464
  9. Creating thumbnail images
    860
  10. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    847
  11. How to speed up access to the RichTextBox Lines property
    828
  12. Window flashing
    378
  13. How to use TTF Font Without Installing
    1,113
  14. How to show window contents while dragging
    451
  15. How to save text from Clipboard to a file
    566
  16. How to restrict mouse movement
    407
  17. List Devices (LPT, COM ports, ...)
    477
  18. How to know whether the font is TrueTypeFont or not
    374
  19. How to track a user's idle time
    587
  20. How to remove your application from TaskBar
    655
  21. How to play sounds on the PC Speaker ?
    524
  22. How to install an INF file using Delphi ?
    460
  23. How to determine the output of a console application ?
    449
  24. Get text from the control at pos x,y on screen
    465
  25. Get dimensions of usable space on monitor (work area)
    486
  26. Get system info (free memory, etc)
    404
  27. How to Get Filenames from Clipboard ?
    1,502
  28. How to Get Windows OS Version ?
    425
  29. Get Windows Temp directory
    656
  30. Disable ALT+TAB, CTRL+ESC, CTRL+ALT+DEL
    1,823
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,717
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252