Skip to main content

Notifications

Announcements

No record found.

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);
}
 
 
  • Suggested answer
    Pradeep Rai Profile Picture
    5,487 Super User 2025 Season 1 on at
    Is keypress event works in Dynamics 365
    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:
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    Is keypress event works in Dynamics 365
    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
     
  • Community member Profile Picture
    2 on at
    Is keypress event works in Dynamics 365
     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
    Adrian Begovich Profile Picture
    1,025 Super User 2025 Season 1 on at
    Is keypress event works in Dynamics 365
    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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 47

Overall leaderboard

Product updates

Dynamics 365 release plans