Chronic pain can deplete the hell out of your endorphins. Sugar, simple carbs, alcohol etc all will give you a quick zap of endorphin effect, so I'd say the pain itself can cause carb cravings.
I don't know about the painkillers themselves, but if it's in the side effect list, then it probably knocks some chemical system out of whack. There's a LOT of them in there.
I'm on like, 3 antidepressants and I'm in ED recovery, so figuring out and managing weight gain due to meds is something I've had to deal with. Here's what works for me:
-The understanding that, at bottom, the ONLY thing that causes weight gain is calorie surplus. I'll probably get some heat for this here, but imo, calories do count. Even PB works mostly by cutting out foods that tend to make you eat more and more (carbs, grains, sugars) and focusing on foods that promote satiety and are hard to overeat: protein and fat, low carb veg, etc. But it all comes down to calories in the end. Various meds can mess somewhat with your metabolic rate, but again- in the end, in most cases, it is calories. A med that makes you gain weight is a med that just makes you eat more. More food=more calories=calorie surplus=weight gain. When it's that simple, it's less scary because it's not unpredictable and mysterious anymore. You know WHY it's happening, and you can do something about it immediately.
-You can manage the situation just by monitoring your weight weekly or even daily (just get a baseline going for daily variations in hydration, etc) and keeping your calories in balance or deficit (if you want to lose weight). There's BMR calculators on the web, just google for it. Get one with an activity multiplier and also (if I get your username right and you're a gal), one for women, and that measures age variation too. Then just stay in balance or under. Not too much under, though. That WILL cause cravings for everything, because starvation makes your brain freak out and usually it starts demanding really high-calorie, high-sugar, high-fat stuff to get you more food nownownow. That's counterproductive.
-A knee injury can make exercise difficult, so you may also have lost some of your daily activity that would otherwise burn more calories. That can cause weight gain- because you've fallen into calorie surplus- if you continue to eat the same way you did. You could just add some light weightlifting with your arms, though. Maybe add that while you watch a favorite tv program or something. You can easily do it sitting with your knee up.
-not having crap carbs in the house is a good idea. But also proactively DO have some other primal-friendly replacement. Then it's just easier to have the primal snack than go through the hassle of going out or getting someone else to get it for you.
-keep a diary of your cravings- time of onset, your mood, any possible emotional/stress causality, how bad the pain is, etc. Just being AWARE helps a lot, and people just don't remember all these details unless they keep a record. You don't have to do this forever, just until you get a good picture of what's going on. You'll probably see patterns and cause-effect stuff fairly shortly. Also put in whatever primal or alternative snack you had instead of a junk carb snack, and whether it worked. Put down the time when your craving ceased. Sometimes try just ignoring the craving for an hour- do something else to keep you busy- can make it fade on it's own. Sometimes it will persist. But that's important data too. There's a lot of anxiety and urgency associated with cravings. If you've written down 'I had coconut oil and nuts instead and that killed the craving' a lot lately, it's a lot easier to feel like this 'works' and you'll be more motivated to take that option in the future.
There's also the classic that I recommend to anyone with carb cravings: l-glutamine. Get it either in capsule or powder form at health food stores. I'd actually recommend the powder form, it will hit you faster. Glutamine can kill a carb craving cold.
Getting my Grok on in the Pacific Northwest.
"C is for cookie, that's good enough for me."
"Cookie is a sometimes food."
"Sometimes cookie monster eat APPLE instead of COOKIE. Sometimes eat CARROT."
-Cookie Monster, partially reformed sugarholic