Skip to main content
Microsoft Dynamics CRM forum
Suggested answer

Is keypress event works in Dynamics 365

editSubscribe (0) ShareShare
Posted on by 2
OnLoad Event, I writen the below code.  iam getting error Cannot read properties of null (reading 'addOnKeyPress')
function alertLead(executionContext)
   var formContext = executionContext.getFormContext();
    var nameKeyPressFunction = function () {
           var key = event.keyCode;
  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,483 Super User 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");
            // 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 {
    3. Once done you need upload this control dynamic CRM for this please refer below link:
    You can follow below link to create the PCF Controls:
  • Suggested answer
    Dengliang Li Profile Picture
    Dengliang Li on at
    Is keypress event works in Dynamics 365
    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 ="input[aria-label='First Name']");
        //Determines if this HTML element is null.
            //Adds the KeyPress event to the input text field.
        //If null, refetch every 10 milliseconds until not null.
    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
  • 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
    Adrian Begovich 20,302 Super User on at
    Is keypress event works in Dynamics 365
    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

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…


Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans