web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Is keypress event works in Dynamics 365

(0) ShareShare
ReportReport
Posted on by 2
OnLoad Event, I writen the below code.  iam getting error Cannot read properties of null (reading 'addOnKeyPress')
 
function alertLead(executionContext)
{
    alert(/Welcome2/);
   var formContext = executionContext.getFormContext();
   alert(Xrm.Page.getAttribute(/budgetamount/).getValue())
    var nameKeyPressFunction = function () {
           var key = event.keyCode;
           alert(key);
       }
       Xrm.Page.getControl(/budgetamount/).addOnKeyPress(nameKeyPressFunction);
}
 
 
I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at
    Hi,
     
    addOnKeypress is a Deprecated Client API. The recommended replacement is a custom control created using Power Apps component framework.
     
    Deprecated Client API Replacement Client API
    addOnKeyPress Use a custom control created using Power Apps component framework
    fireOnKeyPress Use a custom control created using Power Apps component framework
    removeOnKeyPress Use a custom control created using Power Apps component framework
  • Community member Profile Picture
    2 on at
     Could you please give me any example of Power Apps component framework using Keypress Event for Lead Module in the Sales Trail.  one the Account Name field should be use Alphanumeric not special character. 
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    Hi,
     
    You can get the HTML element for a specific input field. Then add the KeyPress event to this element.
    The code is shown below.
     
    function onload(){
        // Gets the specific input field HTML element.
        let first_name = window.top.document.querySelector("input[aria-label='First Name']");
     
        //Determines if this HTML element is null.
        if(first_name){
           
            //Adds the KeyPress event to the input text field.
            first_name.addEventListener("keypress",(e)=>{
     
                alert(e.code)
            })
        }else{
     
        //If null, refetch every 10 milliseconds until not null.
            setTimeout(onload,10);
        }
    }
     
    When I press the A key in the input text field, it displays a dialog box with the value of the key code.
     
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me .
     
    Best Regards,
    Dengliang Li
     
  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at
    To create and Build PCF controls we need to perform multiple steps:
     
    1. You need to run this command to setup the PCF Control:
    pac pcf init --namespace <namespace> --name <controlname> --template field --run-npm-install
     
    2. After execution multiple files will get created and you need to use index.ts file. Try adding below code:
     
     
    import { IInputs, IOutputs } from "./generated/ManifestTypes";
    
    export class CustomTextboxControl implements ComponentFramework.StandardControl<IInputs, IOutputs> {
    
        private _inputElement: HTMLInputElement;
    
        constructor(private context: ComponentFramework.Context<IInputs>, private notifyOutputChanged: () => void, private container: HTMLDivElement) {
            // Create and append input element to the container
            this._inputElement = document.createElement("input");
            this._inputElement.setAttribute("type", "text");
            this.container.appendChild(this._inputElement);
    
            // Add keypress event listener
            this._inputElement.addEventListener('keypress', this.handleKeyPress.bind(this));
        }
    
        public init(): void {
            // Initialize your control
        }
    
        public updateView(context: ComponentFramework.Context<IInputs>): void {
            // Update your control view
        }
    
        public getOutputs(): IOutputs {
            // Return outputs
            return {};
        }
    
        public destroy(): void {
            // Cleanup code
            this._inputElement.removeEventListener('keypress', this.handleKeyPress.bind(this));
        }
    
        private handleKeyPress(event: KeyboardEvent): void {
          
            alert("test");
        }
    }
     
     
    3. Once done you need upload this control dynamic CRM for this please refer below link:
    https://learn.microsoft.com/en-us/power-apps/developer/component-framework/import-custom-controls
     
    You can follow below link to create the PCF Controls:

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans