[C++] Win32 API Key press getting sent 3 times

I have an edit box, in which I process some of the requests (Forgot the term for it) Everytime I press a valid key, it outputs three times, so pressing 0 outputs 000.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
hEdit=CreateWindowEx(0,_T("Edit"),_T(""),editBox,80, yv,43,20,
    vScrollContent,(HMENU)IDE_LA,hIns,0);
    fnEditWndProcNum=(WNDPROC)SetWindowLong(hEdit, GWL_WNDPROC,(long)fnEditSubClassNum);

/*******/

long fnEditSubClassNum_OnChar(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){

//chkScrollPos(hwnd);//If something is entered, the box should be in view, valid or not.
  switch(wParam){
    case 8:  //[Backspace]
    case 48: //0
    case 49: //1
    case 50: //2
    case 51: //3
    case 52: //4
    case 53: //5
    case 54: //6
    case 55: //7
    case 56: //8
    case 57: //9
      MessageBox(0,0,0,0);
      CallWindowProc(fnEditWndProcNum,hwnd,msg,wParam,lParam);
      break;
    default: //Discard the key
      MessageBeep(0xFFFFFFFF); //Default sound to tell user they did something wrong.
    return 0;
  }
  return 0;
}
Instead of using magic numbers, you should just use the identifiers:

1
2
3
4
5
6
7
8
9
10
11
// blech...
    case 8:  //[Backspace]
    case 48: //0
    case 49: //1
    //...

// better:
    case VK_BACK:
    case '0':
    case '1':
    //... 


Also, you probably should run all other messages to the normal window procedure so you're less likely to screw things up.
It may be better, or cleaner, but that's not the issue.

Also, you probably should run all other messages to the normal window procedure so you're less likely to screw things up.


They are, that's just the function for when a button is pressed.

Code looks good to me. Have you debugged it? How many times does the window procedure get called?

Also, what you want to achieve has already been done. Just add the ES_NUMBER style to the edit box and you're good to go (except for pasted text).
What do you mean debugged? It runs, and it used to work. Today, after not working with it for a while, I added a checkbox, and changed a few other small things, but none of then are related. Also, on a different set of edit boxes that also allows a period, it gets sent four times.
I mean debugged, as in setting breakpoints to see how many times the window procedure receives the same WM_CHAR message. See what's in the stack when this happens.
It's called three times in one area, four times in another. The difference being that one allows a period, and the other just allows numerals.
Ok. What's in the stack when this happens? Does the stack show you who issued the second and third WM_CHAR messages?
No, this is all it shows: [PrgName]!fnEditSubClassNum(HWND__ * hwnd=0x000e0884, unsigned int msg=258, unsigned int wParam=50, long lParam=5242881) Line 141 C++
I guess that means the message is being pumped in from outside your program. Some other program using SendMessage()?

Anyway, it is very hard for me to diagnose by asking for pieces of information. You should continue to debug in order to find out where this duplication comes from. Just make sure you return zero when you process WM_CHAR's.
It's a stand alone program. The odd part is, that it wasn't happening before, and while I was testing, it stopped doing it for a second, then when I re-ran the program, it was happening again.

Also, it appears to be four now, not three and four. :s
After a while of experimenting, I found that if I change the parent on the editbox, that the issue goes away, but I've been unable to further isolate the problem. The sub classed proc for the current parent simply enables Page Up/Down to work, and if I remove that, it doesn't fix it. Would you be willing to take a look at the entire thing?
It ended up being a completely unrelated piece of code in the message loop. Solved now.
Topic archived. No new replies allowed.